private function writeResponse(int $requestId, string $headerData, $stream): void
    {
        $data = $headerData;
        $eof  = false;
        $maxSize = 65535;
        fseek($stream, 0, SEEK_SET);

        do {
            $dataLength = strlen($data);

            if ($dataLength < $maxSize && !$eof && !($eof = feof($stream))) {
                $readLength  = $maxSize - $dataLength;
                $data       .= fread($stream, $readLength);
                $dataLength  = strlen($data);
            }

            $writeSize = min($dataLength, $maxSize);
            $writeData = substr($data, 0, $writeSize);
            $data      = substr($data, $writeSize);

            $this->writeRecord($requestId, DaemonInterface::FCGI_STDOUT, $writeData);
        } while ($writeSize === $maxSize);

        $this->writeRecord($requestId, DaemonInterface::FCGI_STDOUT);
    }