library/Rediska/Connection.php
public function connect()
    {
        if (!$this->isConnected()) {
            $socketAddress = 'tcp://' . $this->getHost() . ':' . $this->getPort();

            if ($this->_options['persistent']) {
                $flag = STREAM_CLIENT_PERSISTENT | STREAM_CLIENT_CONNECT;
            } else {
                $flag = STREAM_CLIENT_CONNECT;
            }

            $socketParams = array(
                $socketAddress,
                &$errno,
                &$errmsg,
                $this->getTimeout(),
                $flag
            );

            $streamContext = $this->getStreamContext();

            if ($streamContext) {
                $socketParams[] = $streamContext;
            }

            $this->_socket = call_user_func_array('stream_socket_client', $socketParams);

            // Throw exception if can't connect
            if (!is_resource($this->_socket)) {
                $msg = "Can't connect to Redis server on {$this->getHost()}:{$this->getPort()}";
                if ($errno || $errmsg) {
                    $msg .= "," . ($errno ? " error $errno" : "") . ($errmsg ? " $errmsg" : "");
                }

                $this->_socket = null;

                throw new Rediska_Connection_Exception($msg);
            }

            // Set read timeout
            if ($this->_options['readTimeout'] != null) {
                $this->setReadTimeout($this->_options['readTimeout']);
            }

            // Set blocking mode
            if ($this->_options['blockingMode'] == false) {
                $this->setBlockingMode($this->_options['blockingMode']);
            }

            // Send password
            if ($this->getPassword() != '') {
                $auth = new Rediska_Connection_Exec($this, "AUTH {$this->getPassword()}");
                try {
                    $auth->execute();
                } catch (Rediska_Command_Exception $e) {
                    throw new Rediska_Connection_Exception("Password error: {$e->getMessage()}");
                }
            }

            // Set db
            if ($this->_options['db'] !== self::DEFAULT_DB) {
                $selectDb = new Rediska_Connection_Exec($this, "SELECT {$this->_options['db']}");
                try {
                    $selectDb->execute();
                } catch (Rediska_Command_Exception $e) {
                    throw new Rediska_Connection_Exception("Select db error: {$e->getMessage()}");
                }
            }

            return true;
        } else {
            return false;
        }
    }
public function connect()
    {
        if (!$this->isConnected()) {
            $socketAddress = 'tcp://' . $this->getHost() . ':' . $this->getPort();

            if ($this->_options['persistent']) {
                $flag = STREAM_CLIENT_PERSISTENT | STREAM_CLIENT_CONNECT;
            } else {
                $flag = STREAM_CLIENT_CONNECT;
            }

            $socketParams = array(
                $socketAddress,
                &$errno,
                &$errmsg,
                $this->getTimeout(),
                $flag
            );

            $streamContext = $this->getStreamContext();

            if ($streamContext) {
                $socketParams[] = $streamContext;
            }

            $this->_socket = call_user_func_array('stream_socket_client', $socketParams);

            // Throw exception if can't connect
            if (!is_resource($this->_socket)) {
                $msg = "Can't connect to Redis server on {$this->getHost()}:{$this->getPort()}";
                if ($errno || $errmsg) {
                    $msg .= "," . ($errno ? " error $errno" : "") . ($errmsg ? " $errmsg" : "");
                }

                $this->_socket = null;

                throw new Rediska_Connection_Exception($msg);
            }

            // Set read timeout
            if ($this->_options['readTimeout'] != null) {
                $this->setReadTimeout($this->_options['readTimeout']);
            }

            // Set blocking mode
            if ($this->_options['blockingMode'] == false) {
                $this->setBlockingMode($this->_options['blockingMode']);
            }

            // Send password
            if ($this->getPassword() != '') {
                $auth = new Rediska_Connection_Exec($this, "AUTH {$this->getPassword()}");
                try {
                    $auth->execute();
                } catch (Rediska_Command_Exception $e) {
                    throw new Rediska_Connection_Exception("Password error: {$e->getMessage()}");
                }
            }

            // Set db
            if ($this->_options['db'] !== self::DEFAULT_DB) {
                $selectDb = new Rediska_Connection_Exec($this, "SELECT {$this->_options['db']}");
                try {
                    $selectDb->execute();
                } catch (Rediska_Command_Exception $e) {
                    throw new Rediska_Connection_Exception("Select db error: {$e->getMessage()}");
                }
            }

            return true;
        } else {
            return false;
        }
    }
Connect to redis server

@throws Rediska_Connection_Exception
@return boolean
@uses   self::getStreamContext()