anchor.diff:  	URL ->
   	    {E164User, _, E164Host, _, _} = sipurl:parse(URL),
   	    IsMe = homedomain(E164Host),
  +	    %% Try to rewrite the userpart of the returned URL into a E164 number
  +	    %% to make sure it is not a loop back to this proxy. If it is a remote
  +	    %% domain, the username comparison test does not have any effect, so
  +	    %% a remote URL with the E164 number as uesrname is OK.
   	    NewE164 = rewrite_potn_to_e164(E164User),
   	    SameE164 = util:casecompare(NewE164, "+" ++ E164),
   	    if
   		IsMe /= true ->
   		    logger:log(debug, "Lookup: ENUM lookup resulted in remote URL ~p, relaying", [URL]),
   		    {relay, sipurl:parse(URL)};
  -		NewE164 == none ->
  +		NewE164 == error ->
   		    logger:log(debug, "Lookup: ENUM lookup resulted in a homedomain but not E.164 URL ~p, proxying", [URL]),
   		    {proxy, sipurl:parse(URL)};
   		SameE164 == true ->
   		    logger:log(debug, "Lookup: ENUM lookup resulted in a homedomain (~p) and the same E.164 number (~p == ~p), avoiding loop",
  -			    		[E164Host, E164User, "+" ++ E164]),
  +			       [E164Host, E164User, "+" ++ E164]),
   		    none;
   		true ->
   		    logger:log(debug, "Lookup: ENUM lookup resulted in homedomain E.164 URL ~p, proxying", [URL]),
  
positive.code: 		    none;
  		Unknown ->
  		    logger:log(error, "Lookup: Unexpected results from local:get_telephonenumber_for_user() for user ~p in get_remote_party_number: ~p",
  				[User, Unknown]),
  		    none
  	    end;
  	nomatch ->
  	    logger:log(debug, "Lookup: No user(s) match address ~p, can't get telephone number",
  			[sipurl:print(URL)]),
  	    none;
  	[] ->
  	    logger:log(debug, "Lookup: No user(s) match address ~p, can't get telephone number",
  			[sipurl:print(URL)]),
  	    none;
  	Users when list(Users) ->
  	    logger:log(debug, "Lookup: Multiple users match address ~p, can't get telephone number",
  			[sipurl:print(URL)]),
  	    none;
  	Unknown ->
  	    logger:log(error, "Lookup: Unexpected results from local:get_users_for_url() for URL ~p in get_remote_party_number: ~p",
  			[sipurl:print(URL), Unknown]),
  	    none
      end.
  
  format_number_for_remote_party_id(Number, ToURI, DstHost) ->
      rewrite_potn_to_e164(Number).
  
  get_remote_party_name(Key, URI) when list(Key) ->
      case directory:lookup_tel2name(Key) of
  
negative.code: get_remote_party_name(Key, URI) ->
      logger:log(error, "Lookup: Could not get remote party name for non-list argument ~p", [Key]),
      none.
  
  
  %%--------------------------------------------------------------------
  %%% Internal functions
  %%--------------------------------------------------------------------
  
  %% In some places, we allow lookups to result in URL strings without
  %% protocol. First try to parse them as-is, and if that does not work
  %% then make sure there is no protocol specified that we apparently
  %% do not handle, and if not then prepend them with Proto: and try again.
  parse_url_with_default_protocol(Proto, URLstr) ->
      URL1 = sipurl:parse(URLstr),
      case URL1 of
  	{_, _, _, _, _} ->
  	    URL1;
  	_ ->
  	    case string:chr(URLstr, $@) of
  		0 ->
  		    error;
  		AtIndex ->
  		    %% There is an at-sign in there
  		    UserPart = string:substr(URLstr, 1, AtIndex - 1),
  		    case string:chr(UserPart, $:) of
  			0 ->
  			    %% There is no colon in the userpart of URLstr, try with our default protocol
  			    URL2 = sipurl:parse(Proto ++ ":" ++ URLstr),
  			    case URL2 of
  				{_, _, _, _, _} ->
  				    URL2;
  				_ ->
  				    error
  			    end;
  			_ ->
  			    %% There is already a protocol in URLstr, but apparently not one
  			    %% that sipurl:parse() can handle.
  			    error
  		    end
  	    end
      end.
  