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();
    }
  }
 public void stop() {
   thread.stop();
 }
 public void start() {
   thread.start("email");
 }