public static function createConnection(ConnectionOptions $options)
    {
        $endpoint = $options->getCurrentEndpoint();
        $context = stream_context_create();

        if (Endpoint::getType($endpoint) === Endpoint::TYPE_SSL) {
            // set further SSL options for the endpoint
            stream_context_set_option($context, 'ssl', 'verify_peer', $options[ConnectionOptions::OPTION_VERIFY_CERT]);
            @stream_context_set_option($context, 'ssl', 'verify_peer_name', $options[ConnectionOptions::OPTION_VERIFY_CERT_NAME]);
            stream_context_set_option($context, 'ssl', 'allow_self_signed', $options[ConnectionOptions::OPTION_ALLOW_SELF_SIGNED]);

            if ($options[ConnectionOptions::OPTION_CIPHERS] !== null) {
                // SSL ciphers
                stream_context_set_option($context, 'ssl', 'ciphers', $options[ConnectionOptions::OPTION_CIPHERS]);
            }
        }

        $fp = @stream_socket_client(
            Endpoint::normalize($endpoint),
            $errNo,
            $message,
            $options[ConnectionOptions::OPTION_TIMEOUT],
            STREAM_CLIENT_CONNECT,
            $context
        );

        if (!$fp) {
            throw new ConnectException(
                'cannot connect to endpoint \'' .
                $endpoint . '\': ' . $message, $errNo
            );
        }

        stream_set_timeout($fp, $options[ConnectionOptions::OPTION_TIMEOUT]);

        return $fp;
    }