private void sendP2PMessage(final Object message, final ResponseListener<Object> listener) {
    JSONObject _payload = new JSONObject();

    try {
      _payload.put("type", "p2p");
      _payload.put("to", getFullAppId());
      _payload.put("payload", message);
    } catch (JSONException ex) {
      // do nothing
    }

    if (isConnected()) {
      socket.sendMessage(_payload, null);

      Util.postSuccess(listener, null);
    } else {
      ResponseListener<Object> connectListener =
          new ResponseListener<Object>() {

            @Override
            public void onError(ServiceCommandError error) {
              Util.postError(listener, error);
            }

            @Override
            public void onSuccess(Object object) {
              sendP2PMessage(message, listener);
            }
          };

      connect(connectListener);
    }
  }
  public void handleMediaEvent(JSONObject payload) {
    String type = payload.optString("type");
    if (type.length() == 0) {
      String errorMsg = payload.optString("error");

      if (errorMsg.length() == 0) {
        return;
      } else {
        Log.w(Util.T, "Play State Error: " + errorMsg);
        if (mPlayStateSubscription != null) {
          for (PlayStateListener listener : mPlayStateSubscription.getListeners()) {
            Util.postError(listener, new ServiceCommandError(errorMsg));
          }
        }
      }
    }

    if (type.equals("playState")) {
      if (mPlayStateSubscription == null) return;

      String playStateString = payload.optString(type);
      if (playStateString.length() == 0) return;

      final MediaControl.PlayStateStatus playState = parsePlayState(playStateString);

      for (PlayStateListener listener : mPlayStateSubscription.getListeners()) {
        Util.postSuccess(listener, playState);
      }
    }
  }
  @SuppressWarnings("unchecked")
  public void handleMediaCommandResponse(final JSONObject payload) {
    String requestID = payload.optString("requestId");
    if (requestID.length() == 0) return;

    final ServiceCommand<ResponseListener<Object>> command =
        (ServiceCommand<ResponseListener<Object>>) mActiveCommands.get(requestID);

    if (command == null) return;

    String mError = payload.optString("error");

    if (mError.length() != 0) {
      Util.postError(command.getResponseListener(), new ServiceCommandError(0, mError, null));
    } else {
      Util.postSuccess(command.getResponseListener(), payload);
    }

    mActiveCommands.remove(requestID);
  }