public void connect(final long maxMsToWaitUntilConnected, Watcher watcher) throws ZkInterruptedException,
                                                                              ZkTimeoutException, IllegalStateException {
        boolean started = false;
        try {
            getEventLock().lockInterruptibly();
            setShutdownTrigger(false);
            _eventThread = new ZkEventThread(_connection.getServers());
            _eventThread.start();
            _connection.connect(watcher);

            LOG.debug("Awaiting connection to Zookeeper server");
            if (!waitUntilConnected(maxMsToWaitUntilConnected, TimeUnit.MILLISECONDS)) {
                throw new ZkTimeoutException("Unable to connect to zookeeper server within timeout: "
                                             + maxMsToWaitUntilConnected);
            }
            started = true;
        } catch (InterruptedException e) {
            States state = _connection.getZookeeperState();
            throw new IllegalStateException("Not connected with zookeeper server yet. Current state is " + state);
        } finally {
            getEventLock().unlock();

            // we should close the zookeeper instance, otherwise it would keep
            // on trying to connect
            if (!started) {
                close();
            }
        }
    }
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
shared/common/src/main/java/com/alibaba/otter/shared/common/utils/zookeeper/ZkClientx.java