anchor.diff:      gen_server_init(#state{}).
   
   init([AppId, #transport{listen_ip=Ip, listen_port=Port}=Transp]) ->
  -    Opts = [binary, {reuseaddr, true}, {ip, Ip}, {active, once}],
  -    case gen_udp:open(Port, Opts) of
  +    case open_port(AppId, Ip, Port, 5) of
           {ok, Socket}  ->
               process_flag(priority, high),
               {ok, Port1} = inet:port(Socket),
  -            case gen_tcp:listen(Port1, [{ip, Ip}, {reuseaddr, true}]) of
  -                {ok, TCPSocket} ->
  -                    Self = self(),
  -                    spawn(fun() -> start_tcp(AppId, Ip, Port1, TCPSocket, Self) end),
  -                    Transp1 = Transp#transport{local_port=Port1, listen_port=Port1},
  -                    nksip_proc:put(nksip_transports, {AppId, Transp1}),
  -                    nksip_proc:put({nksip_listen, AppId}, Transp1),
  -                    {ok, 
  -                        #state{
  -                            app_id = AppId, 
  -                            transport = Transp1, 
  -                            socket = Socket,
  -                            stuns = []
  -                    }};
  -                Error ->
  -                    ?error(AppId, "could not start matching TCP transport on ~p:~p: ~p", 
  -                           [Ip, Port1, Error]),
  -                    {stop, no_matching_tcp}
  -            end;
  -        {error, Error} ->
  -            ?error(AppId, "could not start UDP transport on ~p:~p (~p)", 
  +            Self = self(),
  +            spawn(fun() -> start_tcp(AppId, Ip, Port1, Self) end),
  +            Transp1 = Transp#transport{local_port=Port1, listen_port=Port1},
  +            nksip_proc:put(nksip_transports, {AppId, Transp1}),
  +            nksip_proc:put({nksip_listen, AppId}, Transp1),
  +            State = #state{
  +                app_id = AppId, 
  +                transport = Transp1, 
  +                socket = Socket,
  +                tcp_pid = undefined,
  +                stuns = []
  +            },
  +            {ok, State};
  +        {error, Error} ->
  +            ?error(AppId, "B could not start UDP transport on ~p:~p (~p)", 
                      [Ip, Port, Error]),
               {stop, Error}
       end.
  
positive.code: -spec handle_cast(term(), #state{}) ->
      gen_server_cast(#state{}).
  
  handle_cast(no_matching_tcp, State) ->
      {stop, no_matching_tcp, State};
  
  handle_cast(Msg, State) -> 
      lager:warning("Module ~p received unexpected cast: ~p", [?MODULE, Msg]),
  
negative.code: %% ===================================================================
  
  
  % %% @private
  start_tcp(AppId, Ip, Port, Pid) ->
      case nksip_transport:start_transport(AppId, tcp, Ip, Port, []) of
          {ok, TcpPid} -> gen_server:cast(Pid, {matching_tcp, {ok, TcpPid}});
          {error, Error} -> gen_server:cast(Pid, {matching_tcp, {error, Error}})
      end.
  
  %% @private Checks if a port is available for UDP and TCP
  -spec open_port(nksip:app_id(), inet:ip_address(), inet:port_number(), integer()) ->
      {ok, port()} | {error, term()}.
  
  open_port(AppId, Ip, Port, Iter) ->
      Opts = [binary, {reuseaddr, true}, {ip, Ip}, {active, once}],
      case gen_udp:open(Port, Opts) of
          {ok, Socket} ->
              {ok, Socket};
          {error, eaddrinuse} when Iter > 0 ->
              lager:warning("UDP port ~p is in use, waiting (~p)", [Port, Iter]),
              timer:sleep(1000),
              open_port(AppId, Ip, Port, Iter-1);
          {error, Error} ->
              {error, Error}
      end.
  
  
  