protected function connect()
    {
        $errno = null;
        $errstr = null;
        if ( $this->options->connectionType !== self::CONNECTION_PLAIN &&
             !ezcBaseFeatures::hasExtensionSupport( 'openssl' ) )
        {
            throw new ezcBaseExtensionNotFoundException( 'openssl', null, "PHP not configured --with-openssl." );
        }
        if ( count( $this->options->connectionOptions ) > 0 )
        {
            $context = stream_context_create( $this->options->connectionOptions );
            $this->connection = @stream_socket_client( "{$this->options->connectionType}://{$this->serverHost}:{$this->serverPort}",
                                                       $errno, $errstr, $this->options->timeout, STREAM_CLIENT_CONNECT, $context );
        }
        else
        {
            $this->connection = @stream_socket_client( "{$this->options->connectionType}://{$this->serverHost}:{$this->serverPort}",
                                                       $errno, $errstr, $this->options->timeout );
        }

        if ( is_resource( $this->connection ) )
        {
            stream_set_timeout( $this->connection, $this->options->timeout );
            $this->status = self::STATUS_CONNECTED;
            $greeting = $this->getData();
            $this->login();
        }
        else
        {
            throw new ezcMailTransportSmtpException( "Failed to connect to the smtp server: {$this->serverHost}:{$this->serverPort}." );
        }
    }