private void process(EmailCommand command) throws IOException {

    if (connection == null) {
      passResponseToListeners(command.createResponse("NOCON"));
    } else {
      inputBuffer3.delete(0, inputBuffer3.length());

      byte[][] requests = command.getSerialisedCommand();
      String response = null;

      for (int i = 0; i < requests.length; i++) {
        send(requests[i]);

        response = read();
      }

      while (!response.startsWith(command.getCurrentId())) {
        inputBuffer3.append(response);
        inputBuffer3.append(" \n");
        response = read();
      }
      inputBuffer3.append(response);

      passResponseToListeners(command.createResponse(inputBuffer3.toString()));
    }
  }
  public void work() {

    try {
      EmailCommand command = (EmailCommand) eventQueue.pop();
      if (command == null) {
        // ignore
      } else {
        // #debug debug
        System.out.println("Process " + command);

        try {
          if (command instanceof EmailLoginCommand) {
            try {
              connection =
                  (StreamConnection)
                      Connector.open(
                          ((EmailLoginCommand) command).getUrl(), Connector.READ_WRITE, true);
              inbound = connection.openInputStream();
              outbound = connection.openOutputStream();
            } catch (IOException e) {
              // #debug error
              System.out.println("Failed to open connection to email server." + e);
            }
            process(command);
          } else if (command instanceof EmailCloseCommand) {
            try {
              connection.close();
            } catch (IOException e) {
              // #debug error
              System.out.println("Failed to close connection to email server." + e);
            }
            connection = null;
            inbound = null;
            outbound = null;
          } else {
            process(command);
          }
        } catch (IOException e) {
          // #debug error
          System.out.println("Failed sending/receiving command to/from server." + e);

          passResponseToListeners(command.createResponse("IOE"));
        }
      }
    } catch (InterruptedException e) {
      thread.stop();
    }
  }