public static function formatHeaders(array $headers): string
    {
        $buffer = "";
        $lines = 0;

        foreach ($headers as $name => $values) {
            // PHP casts integer-like keys to integers
            $name =  (string) $name;

            // Ignore any HTTP/2 pseudo headers
            if ($name[0] === ":") {
                continue;
            }

            /** @var array $values */
            foreach ($values as $value) {
                $buffer .= "{$name}: {$value}\r\n";
                $lines++;
            }
        }

        $count = \preg_match_all(self::HEADER_REGEX, $buffer);

        if ($lines !== $count || $lines !== \substr_count($buffer, "\n")) {
            throw new InvalidHeaderException("Invalid headers");
        }

        return $buffer;
    }