code: public boolean connect(long maxWait) {
      long timeout = maxWait;
      if (timeout <= 0) {
        timeout = this.connectionTimeout;
      }
      if (this.connector == null) {
        if (!this.setConnector()) {
          log.error("Unable to set up connection to the aggregator.");
          return false;
        }
      }
  
      if (this.session != null) {
        log.error("Already connected!");
        return false;
      }
      long waitTime = timeout;
      do {
        long startAttempt = System.currentTimeMillis();
        this.connector.setConnectTimeoutMillis(waitTime - 5);
        if (this._connect(waitTime)) {
          log.debug("Connection succeeded!");
          return true;
        }
  
        if (this.stayConnected) {
          long retryDelay = this.connectionRetryDelay;
          if (timeout < this.connectionRetryDelay * 2) {
            retryDelay = timeout / 2;
            if (retryDelay < 500) {
              retryDelay = 500;
            }
          }
          try {
            log.warn(String.format(
                "Connection to %s:%d failed, waiting %dms before retrying.",
                this.host, Integer.valueOf(this.port), Long.valueOf(retryDelay)));
            Thread.sleep(retryDelay);
          } catch (InterruptedException ie) {
            // Ignored
          }
          waitTime = waitTime - (System.currentTimeMillis() - startAttempt);
        }
      } while (this.stayConnected && waitTime > 0);
  
      this._disconnect();
      this.finishConnection();
  
      return false;
  
    }
docs: Initiates a connection to the Aggregator (if it is not yet connected). If
  {@code #stayConnected} is {@code true}, then this method will NOT return
  until a connection is established or the timeout is exceeded. A timeout of
  0 means the configured timeout value will be used.
  
  @param maxWait
  how long to wait (in milliseconds) for the connection to
  @return {@code true} if the connection is established within the timeout
  period, else {@code false}.