/** The logic that runs in separate thread. Dispatching responses. */
  public void run() {
    if (connection == null) {
      logger.error("No connection.");
      return;
    }

    try {
      connectionReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

      if (!connectionReader.readLine().contains("XiVO")) {
        logger.error("Error xivo with server!");
        destroy();
        return;
      }

      String line;
      while ((line = connectionReader.readLine()) != null || !stopped) {
        try {
          if (logger.isTraceEnabled()) logger.trace("Read from server:" + line);

          handle((JSONObject) JSONValue.parseWithException(line));
        } catch (Throwable ex) {
          logger.error("Error parsing object:" + line, ex);
        }
      }
    } catch (IOException ex) {
      destroy();
    }
  }
  /** Destroys the server stored list. */
  @Override
  public void destroy() {
    stopped = true;

    try {
      if (connection != null) {
        connection.shutdownInput();
        connection.close();
        connection = null;
      }
    } catch (IOException e) {
    }

    try {
      if (connectionReader != null) {
        connectionReader.close();
        connectionReader = null;
      }
    } catch (IOException ex) {
    }

    if (connectionWriter != null) {
      connectionWriter.close();
      connectionWriter = null;
    }
  }