public void disconnectFromWebApp() {
    connected = false;
    mConnectionListener = null;

    if (appToAppSubscription != null) {
      appToAppSubscription.removeListeners();
      appToAppSubscription = null;
    }

    if (socket != null) {
      socket.setListener(null);
      socket.clearRequests();
      socket.disconnect();
      socket = null;
    }
  }
  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 Boolean isConnected() {
   return connected && socket != null && socket.isConnected();
 }
  private void connect(final Boolean joinOnly, final ResponseListener<Object> connectionListener) {
    if (socket != null && socket.getState() == WebOSTVServiceSocketClient.State.CONNECTING) {
      if (connectionListener != null) {
        connectionListener.onError(
            new ServiceCommandError(
                0,
                "You have a connection request pending,  please wait until it has finished",
                null));
      }

      return;
    }

    if (isConnected()) {
      if (connectionListener != null) connectionListener.onSuccess(null);

      return;
    }

    mConnectionListener =
        new ResponseListener<ServiceCommand<ResponseListener<Object>>>() {

          @Override
          public void onError(ServiceCommandError error) {
            if (socket != null) disconnectFromWebApp();

            if (connectionListener != null) {
              if (error == null) {
                error = new ServiceCommandError(0, "Unknown error connecting to web app", null);
              }

              connectionListener.onError(error);
            }
          }

          @Override
          public void onSuccess(ServiceCommand<ResponseListener<Object>> object) {
            ResponseListener<Object> finalConnectionListener =
                new ResponseListener<Object>() {

                  @Override
                  public void onError(ServiceCommandError error) {
                    disconnectFromWebApp();

                    if (connectionListener != null) connectionListener.onError(error);
                  }

                  @Override
                  public void onSuccess(Object object) {
                    connected = true;

                    if (connectionListener != null) connectionListener.onSuccess(object);
                  }
                };

            service.connectToWebApp(WebOSWebAppSession.this, joinOnly, finalConnectionListener);
          }
        };

    if (socket != null) {
      if (socket.isConnected()) mConnectionListener.onSuccess(null);
      else socket.connect();
    } else {
      socket = new WebOSTVServiceSocketClient(service, WebOSTVServiceSocketClient.getURI(service));
      socket.setListener(mSocketListener);
      socket.connect();
    }
  }