public function connect()
    {
        if (isset($this->_connection)) {
            $this->disconnect();
        }

        $function = $this->_config['persistent'] ? 'pfsockopen' : 'fsockopen';
        $params = [$this->_config['host'], $this->_config['port'], &$errNum, &$errStr];

        if ($this->_config['timeout']) {
            $params[] = $this->_config['timeout'];
        }
        $this->_connection = @call_user_func_array($function, $params);

        if (!empty($errNum) || !empty($errStr)) {
            throw new ConnectionException("{$errNum}: {$errStr} (connecting to {$this->_config['host']}:{$this->_config['port']})");
        }

        $this->connected = is_resource($this->_connection);
        if (!$this->connected) {
            throw new ConnectionException('Connected failed.');
        }

        stream_set_timeout($this->_connection, $this->_config['socket_timeout']);

        return $this->connected;
    }