anchor.diff:  				 parsenaptr(Entry#dns_rr.data)
   			 end,
   	    NAPTRs = lists:map(ParseNAPTR, lists:filter(fun isnaptr/1,
  -					       Rec#dns_rec.anlist)),
  -	    logger:log(debug, "Resolver: naptrlookup: ~p -> found", [Name]),
  +							Rec#dns_rec.anlist)),
  +	    logger:log(debug, "Resolver: naptrlookup: ~p -> found ~p NAPTR record(s)",
  +		       [Name, length(NAPTRs)]),
   	    NAPTRs;
   	{error, E} ->
   	    logger:log(debug, "Resolver: naptrlookup: ~p -> error ~p", [Name, E]),
   	    []
       end.
   
  +
  +%%--------------------------------------------------------------------
  +%% Function: parsenaptr/1
  +%% Description: Takes the result of inet_res:nslookup and parses it
  +%%              into a naptrrecord.
  +%% Returns: NAPTRrecord
  +%%--------------------------------------------------------------------
   parsenaptr(Record) ->
       Binary = list_to_binary(Record),
       <<Order:16,
  -    Preference:16,
  -    Flagslength:8,
  -    Rest/binary>> = Binary,
  +     Preference:16,
  +     Flagslength:8,
  +     Rest/binary>> = Binary,
       <<Flags:Flagslength/binary-unit:8,
  -    Serviceslength:8,
  -    Rest2/binary>> = Rest,
  +     Serviceslength:8,
  +     Rest2/binary>> = Rest,
       <<Services:Serviceslength/binary-unit:8,
  -    Regexplength:8,
  -    Rest3/binary>> = Rest2,
  +     Regexplength:8,
  +     Rest3/binary>> = Rest2,
       <<Regexp:Regexplength/binary-unit:8,
  -    Replacementlength:8,
  -    Rest4/binary>> = Rest3,
  -    <<Replacement:Replacementlength/binary-unit:8>> = Rest4,
  -    {Order,
  -     Preference,
  -     binary_to_list(Flags),
  -     httpd_util:to_upper(binary_to_list(Services)),
  -     binary_to_list(Regexp),
  -     binary_to_list(Replacement)}.
  +     Replacementlength:8,
  +     Rest4/binary>> = Rest3,
  +    %% What we have in Replacementlength is the length of the first
  +    %% (and perhaps only) element of the replacement. It is like a
  +    %% linked list of elements - let parsenaptr_replacement parse it
  +    Replacement = parsenaptr_replacement(Replacementlength, Rest4, []),
  +    #naptrrecord{order=Order, preference=Preference,
  +		 flags=binary_to_list(Flags),
  +		 services=httpd_util:to_upper(binary_to_list(Services)),
  +		 regexp=binary_to_list(Regexp),
  +		 replacement=Replacement
  +		}.
  +
  +%%--------------------------------------------------------------------
  +%% Function: parsenaptr_replacement/1
  +%% Description: Part of parsenaptr/1. Parse the replacement part of
  +%%              the inet:nslookup result into a string.
  +%% Returns: ReplacementString
  +%%--------------------------------------------------------------------
  +parsenaptr_replacement(0, _, Res) ->
  +    Res;
  +parsenaptr_replacement(Len, In, Res) when integer(Len), binary(In) ->
  +    %% Get next element (part of label), if the whole replacement is "_sip._tcp.example.com",
  +    %% then this element will be "_sip", "_tcp", "example" and "com". Concatenate all those
  +    %% parts with dots in between.
  +    <<Elem:Len/binary-unit:8,
  +     NextLen:8,
  +     Rest/binary>> = In,
  +    NewRes = case Res of
  +		 [] -> binary_to_list(Elem);
  +		 _ -> lists:flatten([Res, $., binary_to_list(Elem)])
  +	     end,
  +    parsenaptr_replacement(NextLen, Rest, NewRes).
  +
  +
  +%%--------------------------------------------------------------------
  +%% Function: filter_naptr/3
  +%% Description: Pick out all NAPTR records we recognize as domain
  +%%              NAPTR records from a list of naptrrecord records.
  +%% Returns: DomainNAPTRList
  +%%--------------------------------------------------------------------
  +filter_naptr([], Flag, Service) ->
  +    [];
  +filter_naptr([Elem | Rest], Flag, Service) when record(Elem, naptrrecord), Elem#naptrrecord.flags == Flag ->
  +    case Elem#naptrrecord.services of
  +	Service ->
  +	    %% exact match of Service
  +	    [Elem | filter_naptr(Rest, Flag, Service)];
  +	_ ->
  +	    filter_naptr(Rest, Flag, Service)
  +    end;
  +filter_naptr([H | Rest], Flag, Service) when record(H, naptrrecord), H#naptrrecord.flags /= Flag ->
  +    logger:log(debug, "Resolver: Skipping domain NAPTR because flags is not '~s' : ~p", [Flag, H]),
  +    filter_naptr(Rest, Flag, Service);
  +filter_naptr([H | Rest], Flag, Service) when record(H, naptrrecord), H#naptrrecord.replacement /= "" ->
  +    logger:log(debug, "Resolver: Skipping domain NAPTR because replacement is not empty : ~p", [H]),
  +    filter_naptr(Rest, Flag, Service).
  
positive.code: 	    Rewrite
      end.
  
  enumalldomains(Number, []) ->
      [];
  
  enumalldomains(Number, [Domain | Rest]) ->
      naptrlookup(number2enum(lists:reverse(Number), Domain)) ++
  	enumalldomains(Number, Rest).
  
  chooseenum([], Type) ->
      [];
  chooseenum([Elem | Rest], Type) ->
      case Elem of
  	{Order, Preference, "u", Type, Regexp, ""} ->
  	    %% exact match of Type
  	    [{Order, Preference, Regexp} | chooseenum(Rest, Type)];
  	{Order, Preference, "u", OtherType, Regexp, ""} ->
  	    TypeColon = Type ++ ":",
  	    case string:substr(OtherType, 1, length(Type) + 1) of
  		TypeColon ->
  		    %% Type matches, but there is also a subtype
  		    [{Order, Preference, Regexp} | chooseenum(Rest, Type)];
  		_ ->
  		    chooseenum(Rest, Type)
  	    end;
  	_ ->
  	    chooseenum(Rest, Type)
      end.
  
  sortenum({Order1, Preference1, Regexp1}, {Order2, Preference2, Regexp2}) ->
      if
  	Order1 < Order2 ->
  	    true;
  	true ->
  	    false
      end.
  
  enumregexp([]) ->
      [];
  enumregexp([{Order1, Preference1, Regexp1} | Rest]) ->
      [Regexp1 | enumregexp(Rest)].
  
  enumlookup(none) ->
      none;
  enumlookup("+" ++ Number) ->
  
negative.code:     Res;
  combine_srvresults([{error, What} | T], Res, Errors) ->
      combine_srvresults(T, Res, lists:append(Errors, [{error, What}]));
  combine_srvresults([H | T], Res, Errors) when record(H, srventry) ->
      {_, _, Port, Host} = H#srventry.dnsrrdata,
      combine_srvresults(T, lists:append(Res, [{H#srventry.proto, Host, Port}]), Errors).
  
  
  %%--------------------------------------------------------------------
  %% Function: sortsrv/2
  %% Description: Walk through a list of srventry records or {error, R}
  %%              tuples and sort them according to order and secondly
  %%              preference. Sort errors last, but put nxdomain errors
  %%              first of the errors.
  %% Returns: SRVList
  %%--------------------------------------------------------------------
  sortsrv(A, B) when record(A, srventry), record(B, srventry) ->
      {Order1, Preference1, Port1, Host1} = A#srventry.dnsrrdata,
      {Order2, Preference2, Port2, Host2} = B#srventry.dnsrrdata,
      if
  	Order1 < Order2 ->
  	    true;
  