public function connect()
    {
        $nodes = $this->nodeUrls;

        while (count($nodes)) {
            // pick random server
            $idx = rand(0, count($nodes) - 1);

            try {
                $stream = new DefaultStream($nodes[$idx], $this->log);
                $stream->connect();

                return $stream;
            } catch (ConnectException $e) {
                $this->log->warning($e->getMessage());
            }

            // remove the selected server from the list
            array_splice($nodes, $idx, 1);
        }

        throw new ConnectException('unable to connect to any of ['.implode(',', $this->nodeUrls).']');
    }