private function respond(Request $request, string $buffer): \Generator
    {
        try {
            $method = $request->getMethod();

            if (!\in_array($method, $this->options->getAllowedMethods(), true)) {
                if (!\in_array($method, HttpDriver::KNOWN_METHODS, true)) {
                    $response = yield from $this->makeNotImplementedResponse();
                } else {
                    $response = yield from $this->makeMethodNotAllowedResponse();
                }
            } elseif ($method === "OPTIONS" && $request->getUri()->getPath() === "") {
                $response = $this->makeOptionsResponse();
            } else {
                $response = yield $this->requestHandler->handleRequest(clone $request);

                if (!$response instanceof Response) {
                    throw new \Error("At least one request handler must return an instance of " . Response::class);
                }
            }
        } catch (ClientException $exception) {
            $this->close();
            return;
        } catch (\Throwable $exception) {
            $this->logger->error($exception);
            $response = yield from $this->makeExceptionResponse($request);
        } finally {
            $this->pendingResponses--;
        }

        if ($this->status & self::CLOSED_WR) {
            return; // Client closed before response could be sent.
        }

        $promise = $this->httpDriver->write($request, $response);

        if ($response->isUpgraded()) {
            yield $promise; // Wait on writing response when the response is an upgrade response.
            $this->export($response->getUpgradeCallable(), $buffer);
        }
    }