public function connect($shardId = null)
    {
        if ($shardId === null && $this->_conn) {
            return false;
        }

        if ($shardId !== null && $shardId === $this->activeShardId) {
            return false;
        }

        if ($this->getTransactionNestingLevel() > 0) {
            throw new ShardingException('Cannot switch shard when transaction is active.');
        }

        $activeShardId = $this->activeShardId = (int) $shardId;

        if (isset($this->activeConnections[$activeShardId])) {
            $this->_conn = $this->activeConnections[$activeShardId];

            return false;
        }

        $this->_conn = $this->activeConnections[$activeShardId] = $this->connectTo($activeShardId);

        if ($this->_eventManager->hasListeners(Events::postConnect)) {
            $eventArgs = new ConnectionEventArgs($this);
            $this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
        }

        return true;
    }