anchor.diff:  %% protocol_version string, the infohash the remote sent us and his
   %% peer_id.
   %% --------------------------------------------------------------------
  -receive_header(Socket) ->
  +receive_header(Socket, InfoHash) ->
  +    %% Last thing we do on the socket, catch an error here.
       case gen_tcp:recv(Socket, ?HANDSHAKE_SIZE, ?DEFAULT_HANDSHAKE_TIMEOUT) of
  -	{ok, Packet} ->
  -	    case Packet of
  -		<<PSL:8/integer, ?PROTOCOL_STRING, ReservedBytes:8/binary,
  -		 IH:20/binary, PI:20/binary>> ->
  -		    if
  -			PSL /= length(?PROTOCOL_STRING) ->
  -			    {error, packet_size_mismatch};
  -			true ->
  -			    {ok, ReservedBytes, IH, PI}
  -		    end;
  -		X when is_binary(X) ->
  -		    {error, {bad_header, X}}
  -	    end;
  -	{error, X} ->
  -	    {error, X}
  +	%% Fail if the header length is wrong
  +	{ok, <<PSL:8/integer, ?PROTOCOL_STRING, _:8/binary,
  +	       _IH:20/binary, _PI:20/binary>>}
  +	  when PSL /= length(?PROTOCOL_STRING) ->
  +	    {error, packet_size_mismatch};
  +	%% If the infohash is await, return the infohash along.
  +	{ok, <<_PSL:8/integer, ?PROTOCOL_STRING, ReservedBytes:8/binary,
  +	       IH:20/binary, PI:20/binary>>}
  +	  when InfoHash =:= await ->
  +	    {ok, ReservedBytes, IH, PI};
  +	%% Infohash mismatches. Error it.
  +	{ok, <<_PSL:8/integer, ?PROTOCOL_STRING, _ReservedBytes:8/binary,
  +	       IH:20/binary, _PI:20/binary>>}
  +	  when IH /= InfoHash ->
  +	    {error, infohash_mismatch};
  +	%% Everything ok
  +	{ok, <<_PSL:8/integer, ?PROTOCOL_STRING, ReservedBytes:8/binary,
  +	       _IH:20/binary, PI:20/binary>>} ->
  +	    {ok, ReservedBytes, PI};
  +	%% This is not even a header!
  +	{ok, X} when is_binary(X) ->
  +	    {error, {bad_header, X}};
  +	%% Propagate errors upwards, most importantly, {error, closed}
  +	{error, Reason} ->
  +	    {error, Reason}
       end.
   
   %%--------------------------------------------------------------------
  
positive.code: 
  %%--------------------------------------------------------------------
  %% Function: receive_header(socket()) -> {ok, proto_version(),
  %%                                            info_hash(),
  %%                                            remote_peer_id()} |
  %%                                       {error, Reason}
  %% Description: Recieve the full header from a peer. The function
  
negative.code:     Header = build_peer_protocol_header(),
      case gen_tcp:send(Socket, Header) of
  	ok ->
  	    receive_header(Socket, await);
  	{error, X}  ->
  	    {error, X}
      end.
  
  %%--------------------------------------------------------------------
  %% Function: complete_handshake_header(socket(),
  %%   info_hash(), peer_id()) -> ok | {error, Reason}
  %% Description: Complete the handshake with the peer in the other end.
  %%--------------------------------------------------------------------
  complete_handshake_header(Socket, InfoHash, LocalPeerId) ->
      gen_tcp:send(Socket, InfoHash),
      gen_tcp:send(Socket, LocalPeerId).
  
  
  %%--------------------------------------------------------------------
  %% Function: initiate_handshake(socket(), peer_id(), info_hash()) ->
  