public static function connect(string $host, int $port = 119, array $options = []): Generator
    {
        // Connect to the remote host.
        $socket = yield from Dns\connect($host, $port, $options);

        // Encoder for encoding and decoding the protocol.
        $encoder = new Rfc3977Encoder();

        // Create the client object.
        $client = new static($socket, $encoder);

        // The server should respond with a welcome message.
        $response = yield from $encoder->readResponse($socket);

        // A 201 response code means posting is not allowed.
        if ($response->code() === 201) {
            $client->postingAllowed = false;
        }

        // Get the server capabilities.
        yield from $client->getServerCapabilities();

        // Set reader client mode if necessary.
        if ($client->hasCapability('MODE-READER') || !$client->hasCapability('READER')) {
            yield from $client->sendCommand(new Command('MODE READER'));
        }

        // Enable encryption if supported.
        if ($client->hasCapability('STARTTLS')) {
            yield from $client->sendCommand(new Command('STARTTLS'));
            yield from $socket->enableCrypto(STREAM_CRYPTO_METHOD_ANY_CLIENT);
        }

        return $client;
    }