private function connect(array &$connections)
    {
        $exceptions = [];
        while (count($connections) > 0) {
            /** @var Connection $connection */
            $connection = array_pop($connections);

            try {
                if ($connection->open()) {
                    return $connection;
                }
            } catch (DatabaseException $e) {
                $exceptions[] = $e->getMessage();
            }
        }

        throw new ConnectionException(
            'Unable to select connection.',
            '',
            0,
            count($exceptions) > 0 ? new ConnectionException(implode(' ', $exceptions)) : null
        );
    }