src/NetworkSocket.php
public function enableCrypto(int $method, float $timeout = 0): \Generator
    {
        $resource = $this->getResource();

        if ($method & 1 || 0 === $method) {
            yield from $this->await($timeout);
        } else {
            yield from $this->poll($timeout);

            $raw = stream_socket_recvfrom($resource, 11, STREAM_PEEK);
            if (11 > strlen($raw)) {
                throw new FailureException('Failed to read crypto handshake.');
            }

            $data = unpack('ctype/nversion/nlength/Nembed/nmax-version', $raw);
            if (0x16 !== $data['type']) {
                throw new FailureException('Invalid crypto handshake.');
            }

            $version = $this->selectCryptoVersion($data['max-version']);
            if ($method & $version) { // Check if version was available in $method.
                $method = $version;
            }
        }

        do {
            // Error reporting suppressed since stream_socket_enable_crypto() emits E_WARNING on failure.
            $result = @stream_socket_enable_crypto($resource, (bool) $method, $method);
        } while (0 === $result && !yield from $this->poll($timeout));

        if ($result) {
            $this->crypto = $method;
            return;
        }

        $message = 'Failed to enable crypto.';
        if ($error = error_get_last()) {
            $message .= sprintf(' Errno: %d; %s', $error['type'], $error['message']);
        }
        throw new FailureException($message);
    }
public function enableCrypto(int $method, float $timeout = 0): \Generator
    {
        $resource = $this->getResource();

        if ($method & 1 || 0 === $method) {
            yield from $this->await($timeout);
        } else {
            yield from $this->poll($timeout);

            $raw = stream_socket_recvfrom($resource, 11, STREAM_PEEK);
            if (11 > strlen($raw)) {
                throw new FailureException('Failed to read crypto handshake.');
            }

            $data = unpack('ctype/nversion/nlength/Nembed/nmax-version', $raw);
            if (0x16 !== $data['type']) {
                throw new FailureException('Invalid crypto handshake.');
            }

            $version = $this->selectCryptoVersion($data['max-version']);
            if ($method & $version) { // Check if version was available in $method.
                $method = $version;
            }
        }

        do {
            // Error reporting suppressed since stream_socket_enable_crypto() emits E_WARNING on failure.
            $result = @stream_socket_enable_crypto($resource, (bool) $method, $method);
        } while (0 === $result && !yield from $this->poll($timeout));

        if ($result) {
            $this->crypto = $method;
            return;
        }

        $message = 'Failed to enable crypto.';
        if ($error = error_get_last()) {
            $message .= sprintf(' Errno: %d; %s', $error['type'], $error['message']);
        }
        throw new FailureException($message);
    }
{@inheritdoc}