private void logIn(HostPort hostPort, ConnectHeader header)
      throws AlreadyLoggedInException, NotLoggedInException, MessageListenerHaltException {
    if (loggedInConnections.contains(hostPort))
      throw new AlreadyLoggedInException(hostPort + " is already logged in");

    if (!isForHeader(header))
      throw new NotLoggedInException("login and passcode not specified, cannot log in");

    try {
      getLoginHandler().login(header.getLogin(), header.getPasscode());
      loggedInConnections.add(hostPort);
    } catch (TerminateSessionException e) {
      log.error("Login handler has terminated the session", e);
      sendErrorMessage(e.getMessage(), hostPort);
      gateway.closeConnection(hostPort);
      throw new MessageListenerHaltException();
    }
  }
 private boolean isForHeader(ConnectHeader header) {
   return StringUtils.isNotEmpty(header.getLogin())
       && StringUtils.isNotEmpty(header.getPasscode());
 }