public function parse(Context $context, ?Request $request = null): \Generator
    {
        $header = \unpack('nid/nflags/nqc/nac/nauth/nadd', yield $this->stream->readBuffer($context, 12));
        $this->offset += 12;
        
        $response = new Response($header['id']);
        $response->setFlags($header['flags']);
        
        if ($request && $response->getId() !== $request->getId()) {
            throw new \RuntimeException(\sprintf('Expected DNS message ID is %u, server returned %u', $request->getId(), $response->getId()));
        }
        
        $response->setAuthorityCount($header['auth']);
        $response->setAdditionalCount($header['add']);
        
        // Parse questions:
        for ($i = 0; $i < $header['qc']; $i++) {
            yield from $this->parseQuestion($context, $response);
        }
        
        if ($request && $response->getQuestions() !== $request->getQuestions()) {
            throw new \RuntimeException('DNS server did not return the question(s) in the response message');
        }
        
        // Parse answers:
        for ($i = 0; $i < $header['ac']; $i++) {
            yield from $this->parseAnswer($context, $response);
        }
        
        return $response;
    }