protected function parseHeaders($stream, $uid)
    {
        $rawHeaders = imap_fetchheader($stream, $uid, static::$fetchFlags);

        $headers = [];
        $key = null;

        foreach (explode("\n", $rawHeaders) as $line) {
            // Test if continutation
            if ($line !== '' && !preg_match('/^\s/', $line)) {
                list($key, $val) = explode(':', $line, 2);
                $headers[$key] = trim($val);
            } elseif (!is_null($key) && ($trimmed = trim($line)) !== '') {
                $headers[$key] = $headers[$key].$trimmed;
            }
        }

        foreach($headers as $headerKey => $headerValue) {
            if (preg_match('/(\w+)=(\S+);(?:\s|$)/', $headerValue)) {
                $headers[$headerKey] = $this->parseNestedKeyPairHeader($headerValue);
            }
        }

        return $headers;
    }