code: public void connect(final long maxMsToWaitUntilConnected, Watcher watcher) throws ZkInterruptedException, ZkTimeoutException, IllegalStateException {
          boolean started = false;
          acquireEventLock();
          try {
              setShutdownTrigger(false);
              _eventThread = new ZkEventThread(_connection.getServers());
              _eventThread.start();
              _connection.connect(watcher);
  
              LOG.debug("Awaiting connection to Zookeeper server");
              boolean waitSuccessful = waitUntilConnected(maxMsToWaitUntilConnected, TimeUnit.MILLISECONDS);
              if (!waitSuccessful) {
                  throw new ZkTimeoutException("Unable to connect to zookeeper server '" + _connection.getServers() + "' with timeout of " + maxMsToWaitUntilConnected + " ms");
              }
              started = true;
          } finally {
              getEventLock().unlock();
  
              // we should close the zookeeper instance, otherwise it would keep
              // on trying to connect
              if (!started) {
                  close();
              }
          }
      }
docs: Connect to ZooKeeper.
  
  @param maxMsToWaitUntilConnected
  @param watcher
  @throws ZkInterruptedException
  if the connection timed out due to thread interruption
  @throws ZkTimeoutException
  if the connection timed out
  @throws IllegalStateException
  if the connection timed out due to thread interruption