anchor.diff:  %%           of.
   %%--------------------------------------------------------------------
   test() ->
  -    %% test received_response_state_machine/3
  +    Test_Request =
  +	#request{method = "INVITE",
  +		 uri    = sipurl:parse("sip:user@192.0.2.27"),
  +		 header = keylist:from_list([{"From",    ["Test <sip:test@example.org>;tag=f-tag"]},
  +					     {"To",      ["Receiver <sip:recv@example.org>"]},
  +					     {"CSeq",    ["1 INVITE"]},
  +					     {"Call-Id", ["truly-random"]}
  +					    ]),
  +		 body   = <<"sdp">>
  +		},
  +
  +    Test_Me = lists:concat([siprequest:myhostname(), ":", sipsocket:default_port(yxa_test, none)]),
  +    Test_Response =
  +	#response{status = 100,
  +		  reason = "Testing",
  +		  header = keylist:from_list([{"Via",     ["SIP/2.0/YXA-TEST " ++ Test_Me,
  +							   "SIP/2.0/YXA-TEST 192.0.2.27"]},
  +					      {"From",    ["Test <sip:test@example.org>;tag=f-tag"]},
  +					      {"To",      ["Receiver <sip:recv@example.org>;tag=t-tag"]},
  +					      {"CSeq",    ["1 INVITE"]},
  +					      {"Call-Id", ["truly-random"]}
  +					     ]),
  +		  body   = <<>>
  +		 },
  +    Test_Dst = #sipdst{proto = yxa_test,
  +		       addr  = "192.0.2.27",
  +		       port  = 6050,
  +		       uri   = Test_Request#request.uri
  +		      },
  +
  +    %% unknown handle_call
  +    %%--------------------------------------------------------------------
  +    io:format("test: unknown gen_server:call() - 1~n"),
  +    UnknownCallState = #state{},
  +    {reply, {error, "unknown gen_server call"}, UnknownCallState} =
  +	handle_call(undefined, none, UnknownCallState),
  +
  +
  +    %% handle_cast({sipmessage, Response, Origin, LogStr}, ...)
  +    %%--------------------------------------------------------------------
  +    io:format("test: gen_server:cast() {sipmessage, ...} - 1.1~n"),
  +    SipMessage_State1 = #state{request   = Test_Request,
  +			       sipstate  = calling,
  +			       timerlist = siptimer:empty()
  +			      },
  +    {noreply, SipMessage_State1_out} =
  +	handle_cast({sipmessage, Test_Response, #siporigin{}, "testing"}, SipMessage_State1),
  +
  +    io:format("test: gen_server:cast() {sipmessage, ...} - 1.2~n"),
  +    %% verify new state
  +    proceeding = SipMessage_State1_out#state.sipstate,
  +    100 = (SipMessage_State1_out#state.response)#response.status,
  +    "Testing" = (SipMessage_State1_out#state.response)#response.reason,
  +    SipMessage_State1 = SipMessage_State1_out#state{sipstate = calling,
  +						    response = undefined
  +						   },
  +
  +
  +    %% handle_cast({cancel, Msg, ExtraHeaders}
  +    %%--------------------------------------------------------------------
  +    io:format("test: gen_server:cast() {cancel, ...} - 1~n"),
  +    %% test already cancelled, no change expected
  +    Cancel_State1 = #state{cancelled = true},
  +    {noreply, Cancel_State1} = handle_cast({cancel, "testing", []}, Cancel_State1),
  +
  +    io:format("test: gen_server:cast() {cancel, ...} - 2.1~n"),
  +    %% test normal case, expect that a cancel transaction won't get started since
  +    %% we are in SIP-state 'completed'
  +    Cancel_State2 = #state{cancelled = false,
  +			   sipstate  = completed,
  +			   request   = Test_Request
  +			  },
  +    {noreply, Cancel_State2_out} = handle_cast({cancel, "testing", []}, Cancel_State2),
  +
  +    io:format("test: gen_server:cast() {cancel, ...} - 2.2~n"),
  +    %% verify new state
  +    true = Cancel_State2_out#state.cancelled,
  +    Cancel_State2 = Cancel_State2_out#state{cancelled = false},
  +
  +
  +    %% handle_cast({expired}, ...)
  +    %%--------------------------------------------------------------------
  +    io:format("test: gen_server:cast() {expired} - 1~n"),
  +    ExpiredState = #state{logtag = "testing"},
  +    {stop, "Client transaction expired", ExpiredState} = handle_cast({expired}, ExpiredState),
  +
  +
  +    %% handle_cast({quit}, ...)
  +    %%--------------------------------------------------------------------
  +    io:format("test: gen_server:cast() {quit} - 1~n"),
  +    QuitState = #state{logtag = "testing"},
  +    {stop, normal, QuitState} = handle_cast({quit}, QuitState),
  +
  +
  +    %% unknown handle_cast
  +    %%--------------------------------------------------------------------
  +    io:format("test: unknown gen_server:cast() - 1~n"),
  +    UnknownCastState = #state{},
  +    {noreply, UnknownCastState} = handle_cast(undefined, UnknownCastState),
  +
  +
  +    %% handle_info({siptimer, TRef, TDesc}, State)
  +    %%--------------------------------------------------------------------
  +    io:format("test: gen_server signal '{siptimer, ...}' - 0~n"),
  +    {ok, TimerTest_Signal, SipTimerTest_Timerlist1} = test_get_timer({terminate_transaction}),
  +
  +    TimerTest_State = #state{timerlist = SipTimerTest_Timerlist1,
  +			     request   = Test_Request
  +			    },
  +
  +    io:format("test: gen_server signal '{siptimer, ...}' - 1~n"),
  +    %% test normal case, the transaction should terminate
  +    {stop, normal, #state{sipstate = terminated}} = handle_info(TimerTest_Signal, TimerTest_State),
  +
  +    io:format("test: gen_server signal '{siptimer, ...}' - 1~n"),
  +    %% test with unknown timer
  +    {noreply, TimerTest_State} =
  +	handle_info({siptimer, make_ref(), "Testing unknown timer"}, TimerTest_State),
  +
  +
  +    %% handle_info({'EXIT', Pid, Reason}, State)
  +    %%--------------------------------------------------------------------
  +    io:format("test: gen_server signal '{'EXIT', ...}' - 0~n"),
  +    ExitSignal_State = #state{logtag    = "testing",
  +			      parent    = self(),
  +			      report_to = self(),
  +			      request   = Test_Request,
  +			      timerlist = siptimer:empty(),
  +			      sipstate  = completed
  +			     },
  +
  +    io:format("test: gen_server signal '{'EXIT', ...}' - 1.1~n"),
  +    %% test normal case
  +    {noreply, ExitSignal_State1_out} = handle_info({'EXIT', self(), normal}, ExitSignal_State),
  +
  +    io:format("test: gen_server signal '{'EXIT', ...}' - 1.2~n"),
  +    %% verify new state
  +    none = ExitSignal_State1_out#state.report_to,
  +    ExitSignal_State = ExitSignal_State1_out#state{report_to = self()},
  +
  +    io:format("test: gen_server signal '{'EXIT', ...}' - 2~n"),
  +    %% test normal case with non-normal reason
  +    ExitSignal_State2 = ExitSignal_State#state{report_to = none},
  +    {noreply, ExitSignal_State2} = handle_info({'EXIT', self(), false}, ExitSignal_State2),
  +
  +    io:format("test: gen_server signal '{'EXIT', ...}' - 3.1~n"),
  +    %% test normal case which will result in this transaction 'cancelling'
  +    ExitSignal_State3 = ExitSignal_State#state{sipstate = calling},
  +    {noreply, ExitSignal_State3_out} = handle_info({'EXIT', self(), normal}, ExitSignal_State3),
  +
  +    io:format("test: gen_server signal '{'EXIT', ...}' - 3.2~n"),
  +    %% verify new state
  +    none = ExitSignal_State3_out#state.report_to,
  +    true = ExitSignal_State3_out#state.cancelled,
  +    {true, _} = ExitSignal_State3_out#state.do_cancel,
  +    ExitSignal_State3 = ExitSignal_State3_out#state{report_to = self(),
  +						    cancelled = false,
  +						    do_cancel = false
  +						   },
  +
  +    io:format("test: gen_server signal '{'EXIT', ...}' - 4~n"),
  +    %% test EXIT from cancel_pid, will be ignored
  +    ExitSignal_State4 = #state{parent = none,
  +			       cancel_pid = self()
  +			      },
  +    {noreply, ExitSignal_State4} = handle_info({'EXIT', self(), normal}, ExitSignal_State4),
  +
  +
  +    %% unknown handle_info
  +    %%--------------------------------------------------------------------
  +    io:format("test: unknown gen_server signal - 1~n"),
  +    UnknownInfoState = #state{},
  +    {noreply, UnknownInfoState} = handle_info(undefined, UnknownInfoState),
  +
  +
  +    %% terminate(Reason, State)
  +    %%--------------------------------------------------------------------
  +    io:format("test: terminate/2 - 1.1~n"),
  +    %% test normal case
  +    TerminateState1 = #state{report_to = self(),
  +			     branch = "branch"
  +			    },
  +    normal = terminate(normal, TerminateState1),
  +
  +    io:format("test: terminate/2 - 1.2~n"),
  +    %% verify that report_to (we) got a 'clienttransaction_terminating' signal
  +    TerminateSelf = self(),
  +    receive
  +	{clienttransaction_terminating, TerminateSelf, "branch"} ->
  +	    ok
  +    after 0 ->
  +	    throw("test failed, we never got a clienttransaction_terminating signal")
  +    end,
  +
  +    io:format("test: terminate/2 - 2~n"),
  +    %% test with dead report_to
  +    TerminateDeadPid = spawn(fun() -> ok end),
  +    erlang:yield(),	%% make sure TerminateDeadPid finishes
  +    TerminateState2 = #state{report_to = TerminateDeadPid},
  +    normal = terminate(normal, TerminateState2),
  +
  +
  +    %% check_quit2(Res, From, State)
  +    %%--------------------------------------------------------------------
  +    io:format("test: check_quit2/3 - 1~n"),
  +    %% test Res = {reply, ...} (but From = none)
  +    CheckQuit_State = #state{logtag   = "testing",
  +			     request  = Test_Request,
  +			     response = Test_Response,
  +			     sipstate = terminated
  +			    },
  +    {stop, normal, CheckQuit_State} =
  +	check_quit2({reply, ok, CheckQuit_State}, none, CheckQuit_State),
  +
  +    io:format("test: check_quit2/3 - 2~n"),
  +    %% test {Status, Reason} response
  +    CheckQuit_State2 = CheckQuit_State#state{ response = {400, "Testing"} },
  +    {stop, normal, CheckQuit_State2} =
  +	check_quit2({reply, ok, CheckQuit_State2}, none, CheckQuit_State2),
  +
  +
  +    io:format("test: check_quit2/3 - 3~n"),
  +    %% test Res = {stop, ...}
  +    {stop, true, CheckQuit_State} = check_quit2({stop, true, CheckQuit_State}, none, CheckQuit_State),
  +
  +
  +    %% process_timer2({resendrequest}, Timer, State)
  +    %%--------------------------------------------------------------------
  +    io:format("test: process_timer2/3 {resendrequest} - 0~n"),
  +    {ok, ResendRequestTimer_Signal, ResendRequestTimer_Timerlist1} =
  +	test_get_timer({resendrequest}),
  +
  +    ResendRequestTimer_State =
  +	#state{logtag    = "testing",
  +	       socket    = #sipsocket{proto = yxa_test},
  +	       timerlist = ResendRequestTimer_Timerlist1,
  +	       request   = Test_Request,
  +	       sipstate  = calling,
  +	       dst       = Test_Dst,
  +	       branch    = "test-branch"
  +	      },
  +
  +    io:format("test: process_timer2/3 {resendrequest} - 1.1~n"),
  +    %% test normal case, the transaction should terminate
  +    {noreply, ResendRequestTimer_State_out} =
  +	handle_info(ResendRequestTimer_Signal, ResendRequestTimer_State),
  +
  +    io:format("test: process_timer2/3 {resendrequest} - 1.2~n"),
  +    %% cancel the revived timer
  +    siptimer:cancel_all_timers(ResendRequestTimer_State_out#state.timerlist),
  +    %% verify new state
  +    ResendRequestTimer_State = ResendRequestTimer_State_out#state{timerlist = ResendRequestTimer_Timerlist1},
  +    [{resendrequest}] = siptimer:test_get_appsignals(ResendRequestTimer_State_out#state.timerlist),
  +
  +    test_verify_request_was_sent(Test_Request, "process_timer2/3 {resendrequest}", 1, 3),
  +
  +    io:format("test: process_timer2/3 {resendrequest} - 2~n"),
  +    %% test not in sipstate 'completed', no change expected
  +    %% not really an error but we should not normally end up there either
  +    ResendRequestTimer_State2 = ResendRequestTimer_State#state{sipstate = completed},
  +    {noreply, ResendRequestTimer_State2} =
  +	handle_info(ResendRequestTimer_Signal, ResendRequestTimer_State2),
  +
  +
  +    %% process_timer2({resendrequest_timeout}, Timer, State)
  +    %%--------------------------------------------------------------------
  +    io:format("test: process_timer2/3 {resendrequest_timeout} - 0~n"),
  +    {ok, ResendRequestTimeout_Signal, ResendRequestTimeout_Timerlist1} =
  +	test_get_timer({resendrequest_timeout}),
  +
  +    ResendRequestTimeout_State =
  +	#state{logtag    = "testing",
  +	       socket    = #sipsocket{proto = yxa_test},
  +	       timerlist = ResendRequestTimeout_Timerlist1,
  +	       request   = Test_Request,
  +	       sipstate  = calling,
  +	       report_to = self(),
  +	       testing   = true,
  +	       branch    = "branch"
  +	      },
  +
  +    io:format("test: process_timer2/3 {resendrequest_timeout} - 1.1~n"),
  +    %% test normal case
  +    {stop, normal, ResendRequestTimeout_State_out} =
  +	handle_info(ResendRequestTimeout_Signal, ResendRequestTimeout_State),
  +
  +    test_verify_resendrequest_timeout_result(ResendRequestTimeout_State, ResendRequestTimeout_State_out,
  +					     terminated,
  +					     {408, "Request Timeout"},
  +					     1, 2
  +					    ),
  +
  +    io:format("test: process_timer2/3 {resendrequest_timeout} - 2.1~n"),
  +    %% test normal case with cancelled request
  +    ResendRequestTimeout_State2 =
  +	ResendRequestTimeout_State#state{do_cancel = {true, []},
  +					 sipstate  = calling
  +					},
  +    {stop, normal, ResendRequestTimeout_State_out2} =
  +	handle_info(ResendRequestTimeout_Signal, ResendRequestTimeout_State2),
  +
  +    test_verify_resendrequest_timeout_result(ResendRequestTimeout_State2, ResendRequestTimeout_State_out2,
  +					     terminated,
  +					     {487, "Request Terminated"},
  +					     2, 2
  +					    ),
  +
  +    io:format("test: process_timer2/3 {resendrequest_timeout} - 3.1~n"),
  +    %% test normal case in state 'calling' with cancelled request
  +    ResendRequestTimeout_State3 =
  +	ResendRequestTimeout_State#state{do_cancel = false,
  +					 sipstate  = calling
  +					},
  +    {stop, normal, ResendRequestTimeout_State_out3} =
  +	handle_info(ResendRequestTimeout_Signal, ResendRequestTimeout_State3),
  +
  +    test_verify_resendrequest_timeout_result(ResendRequestTimeout_State3, ResendRequestTimeout_State_out3,
  +					     terminated,
  +					     {408, "Request Timeout"},
  +					     3, 2
  +					    ),
  +
  +    io:format("test: process_timer2/3 {resendrequest_timeout} - 4~n"),
  +    %% test normal case in state 'proceeding', cancelled = true to not actually start a CANCEL transaction
  +    ResendRequestTimeout_State4 =
  +	ResendRequestTimeout_State#state{do_cancel = false,
  +					 sipstate  = proceeding,
  +					 cancelled = true
  +					},
  +    {noreply, ResendRequestTimeout_State4} =
  +	handle_info(ResendRequestTimeout_Signal, ResendRequestTimeout_State4),
  +
  +    io:format("test: process_timer2/3 {resendrequest_timeout} - 5.1~n"),
  +    %% test normal case in case 'proceeding' with non-INVITE request
  +    ResendRequestTimeout_Request5 = (ResendRequestTimeout_State#state.request)#request{method = "OPTIONS"},
  +    ResendRequestTimeout_State5 =
  +	ResendRequestTimeout_State#state{sipstate = proceeding,
  +					 request = ResendRequestTimeout_Request5
  +					},
  +    {stop, normal, ResendRequestTimeout_State_out5} =
  +	handle_info(ResendRequestTimeout_Signal, ResendRequestTimeout_State5),
  +
  +    test_verify_resendrequest_timeout_result(ResendRequestTimeout_State5, ResendRequestTimeout_State_out5,
  +					     terminated,
  +					     {408, "Request Timeout"},
  +					     5, 2
  +					    ),
  +
  +    io:format("test: process_timer2/3 {resendrequest_timeout} - 6~n"),
  +    %% test normal case in state 'proceeding', cancelled = true to not actually start a CANCEL transaction
  +    ResendRequestTimeout_State6 =
  +	ResendRequestTimeout_State#state{do_cancel = false,
  +					 sipstate  = completed
  +					},
  +    {noreply, ResendRequestTimeout_State6} =
  +	handle_info(ResendRequestTimeout_Signal, ResendRequestTimeout_State6),
  +
  +
  +    %% process_timer2({invite_timeout}, Timer, State)
  +    %%--------------------------------------------------------------------
  +    io:format("test: process_timer2/3 {invite_timeout} - 0~n"),
  +    {ok, InviteTimeout_Signal, InviteTimeout_Timerlist1} =
  +	test_get_timer({invite_timeout}),
  +
  +    InviteTimeout_State =
  +	#state{logtag    = "testing",
  +	       socket    = #sipsocket{proto = yxa_test},
  +	       timerlist = InviteTimeout_Timerlist1,
  +	       request   = Test_Request,
  +	       sipstate  = calling,
  +	       report_to = self(),
  +	       testing   = true,
  +	       branch    = "branch",
  +	       cancelled = true
  +	      },
  +
  +    io:format("test: process_timer2/3 {invite_timeout} - 1.1~n"),
  +    %% test normal case, a 408 Request Timeout response should be generated
  +    {noreply, InviteTimeout_State_out} =
  +	handle_info(InviteTimeout_Signal, InviteTimeout_State),
  +
  +    io:format("test: process_timer2/3 {invite_timeout} - 1.2~n"),
  +    %% verify new state
  +    {408, "Request Timeout"} = InviteTimeout_State_out#state.response,
  +    InviteTimeout_State = InviteTimeout_State_out#state{response     = undefined,
  +							final_r_sent = false
  +						       },
  +
  +    io:format("test: process_timer2/3 {invite_timeout} - 1.3~n"),
  +    %% verify that report_to was notified
  +    Self = self(),
  +    receive
  +	{branch_result, Self, "branch", calling, {408, "Request Timeout"}} ->
  +	    ok
  +    after 0 ->
  +	    throw("test failed, report_to was not notified about branch result")
  +    end,
  +
  +
  +    %% process_timer2({invite_expire}, Timer, State)
  +    %%--------------------------------------------------------------------
  +    io:format("test: process_timer2/3 {invite_expire} - 0~n"),
  +    {ok, InviteExpire_Signal, InviteExpire_Timerlist1} =
  +	test_get_timer({invite_expire}),
  +
  +    InviteExpire_State =
  +	#state{logtag    = "testing",
  +	       socket    = #sipsocket{proto = yxa_test},
  +	       timerlist = InviteExpire_Timerlist1,
  +	       request   = Test_Request,
  +	       sipstate  = calling,
  +	       report_to = self(),
  +	       testing   = true,
  +	       branch    = "branch",
  +	       cancelled = false
  +	      },
  +
  +    io:format("test: process_timer2/3 {invite_expire} - 1.1~n"),
  +    %% test normal case, a 408 Request Timeout response should be generated
  +    {stop, normal, InviteExpire_State_out} =
  +	handle_info(InviteExpire_Signal, InviteExpire_State),
  +
  +    io:format("test: process_timer2/3 {invite_expire} - 1.2~n"),
  +    %% verify new state
  +    {408, "Request Timeout"} = InviteExpire_State_out#state.response,
  +    InviteExpire_State = InviteExpire_State_out#state{response     = undefined,
  +						      final_r_sent = false,
  +						      sipstate     = calling,
  +						      timerlist    = InviteExpire_State#state.timerlist
  +						     },
  +
  +    io:format("test: process_timer2/3 {invite_expire} - 1.2~n"),
  +    %% verify that report_to was notified
  +    Self = self(),
  +    receive
  +	{branch_result, Self, "branch", terminated, {408, "Request Timeout"}} ->
  +	    ok
  +    after 0 ->
  +	    throw("test failed, report_to was not notified about branch result")
  +    end,
  +
  +
  +    %% process_timer2(Signal, Timer, State)
  +    %%--------------------------------------------------------------------
  +    io:format("test: process_timer2/3 Unknown - 0~n"),
  +    {ok, UnknownTimer_Signal, UnknownTimer_Timerlist1} =
  +	test_get_timer(testing),
  +    UnknownTimer_State =
  + 	#state{logtag    = "testing",
  +	       timerlist = UnknownTimer_Timerlist1
  +	      },
  +
  +    io:format("test: process_timer2/3 Unknown - 1~n"),
  +    {noreply, UnknownTimer_State} = handle_info(UnknownTimer_Signal, UnknownTimer_State),
  +
  +
  +    %% process_received_response(Response, State)
  +    %%--------------------------------------------------------------------
  +    io:format("test: process_received_response/2 - 1.0~n"),
  +    put({sipsocket_test, is_reliable_transport}, false),
  +    PRRes_State1 = #state{logtag    = "testing",
  +			  socket    = #sipsocket{proto = yxa_test,
  +						 module = sipsocket_test
  +						},
  +			  branch    = "branch",
  +			  tl_branch = "tl_branch",
  +			  dst       = Test_Dst,
  +			  request   = Test_Request,
  +			  sipstate  = calling,
  +			  report_to = self(),
  +			  timerlist = siptimer:empty(),
  +			  testing   = true
  +			 },
  +    PPRes_Response1 = Test_Response#response{status = 400,
  +					     reason = "Testing"
  +					    },
  +
  +    io:format("test: process_received_response/2 - 1.1~n"),
  +    PRRes_State1_out = process_received_response(PPRes_Response1, PRRes_State1),
  +
  +    io:format("test: process_received_response/2 - 1.2~n"),
  +    %% verify new state
  +    siptimer:cancel_all_timers(PRRes_State1_out#state.timerlist),
  +    400 = (PRRes_State1_out#state.response)#response.status,
  +    "Testing" = (PRRes_State1_out#state.response)#response.reason,
  +    [{terminate_transaction}] = siptimer:test_get_appsignals(PRRes_State1_out#state.timerlist),
  +    completed = PRRes_State1_out#state.sipstate,
  +
  +    %% verify the ACK sent
  +    {ok, PPRes_Request1_out} = test_verify_request_was_sent(Test_Request#request{method = "ACK", body = <<>>},
  +							    "process_received_response/2", 1, 3),
  +
  +    io:format("test: process_received_response/2 - 1.6~n"),
  +    %% verify that From, To and Call-Id was copied verbatim
  +    lists:map(fun(H) ->
  +		      OrigVal = keylist:fetch(H, Test_Response#response.header),
  +		      AckVal = keylist:fetch(H, PPRes_Request1_out#request.header),
  +		      OrigVal = AckVal
  +	      end, ['from', 'to', 'call-id']),
  +    ["1 ACK"] = keylist:fetch('cseq', PPRes_Request1_out#request.header),
  +
  +    io:format("test: process_received_response/2 - 1.7~n"),
  +    %% verify that report_to was notified
  +    Self = self(),
  +    receive
  +	{branch_result, Self, "branch", completed, #response{status = 400, reason = "Testing"}} ->
  +	    ok
  +    after 0 ->
  +	    throw("test failed, report_to was not notified about branch result")
  +    end,
  +
  +    io:format("test: process_received_response/2 - 1.8~n"),
  +    %% verify that the ACK sent had a single Via (ours)
  +    [PPRes_MyVia] = keylist:fetch('via', PPRes_Request1_out#request.header),
  +
  +    io:format("test: process_received_response/2 - 1.9~n"),
  +    %% verify that it was our tl_branch (and not branch) that was put in the ACK's Via
  +    true = (string:str(PPRes_MyVia, PRRes_State1#state.tl_branch) /= 0),
  +
  +    io:format("test: process_received_response/2 - 2.0~n"),
  +    %% test same thing as previous test, but with Route header and alread in SIP-state 'completed'
  +    %% (simulating a resent 4xx response after we sent our ACK)
  +    PPRes_Route2 = ["<sip:route-a.example.com>", "<sip:route-b.example.com>"],
  +    PPRes_Header2 = keylist:set("Route", PPRes_Route2, Test_Request#request.header),
  +    PRRes_State2 = #state{logtag    = "testing",
  +			  socket    = #sipsocket{proto = yxa_test,
  +						 module = sipsocket_test
  +						},
  +			  branch    = "branch",
  +			  tl_branch = "tl_branch",
  +			  dst       = Test_Dst,
  +			  request   = Test_Request#request{header = PPRes_Header2},
  +			  sipstate  = confirmed,
  +			  report_to = self(),
  +			  timerlist = siptimer:empty(),
  +			  testing   = true
  +			 },
  +    PPRes_Response2 = Test_Response#response{status = 401,
  +					     reason = "Testing"
  +					    },
  +
  +    io:format("test: process_received_response/2 - 2.1~n"),
  +    PRRes_State2_out = process_received_response(PPRes_Response2, PRRes_State2),
  +
  +    io:format("test: process_received_response/2 - 2.2~n"),
  +    %% verify new state
  +    401 = (PRRes_State2_out#state.response)#response.status,
  +    "Testing" = (PRRes_State2_out#state.response)#response.reason,
  +    [] = siptimer:test_get_appsignals(PRRes_State2_out#state.timerlist),
  +    confirmed = PRRes_State2_out#state.sipstate,
  +
  +    %% verify the ACK sent
  +    {ok, PPRes_Request2_out} = test_verify_request_was_sent(Test_Request#request{method = "ACK", body = <<>>},
  +							    "process_received_response/2", 2, 3),
  +
  +    io:format("test: process_received_response/2 - 2.6~n"),
  +    %% verify that From, To and Call-Id was copied verbatim
  +    lists:map(fun(H) ->
  +		      OrigVal = keylist:fetch(H, Test_Response#response.header),
  +		      AckVal = keylist:fetch(H, PPRes_Request2_out#request.header),
  +		      OrigVal = AckVal
  +	      end, ['from', 'to', 'call-id']),
  +    ["1 ACK"] = keylist:fetch('cseq', PPRes_Request2_out#request.header),
  +    PPRes_Route2 = keylist:fetch('route', PPRes_Request2_out#request.header),
  +
  +    io:format("test: process_received_response/2 - 2.7~n"),
  +    %% verify that report_to was NOT notified (since we were already in SIP-state 'completed')
  +    receive
  +	{branch_result, Self, "branch", completed, #response{status = 401, reason = "Testing"}} ->
  +	    throw("test failed, report_to was notified when it shouldn't have")
  +    after 0 ->
  +	    ok
  +    end,
  +
  +    io:format("test: process_received_response/2 - 2.8~n"),
  +    %% verify that the ACK sent had a single Via (ours)
  +    [_PPRes_MyVia2] = keylist:fetch('via', PPRes_Request2_out#request.header),
  +
  +
  +    io:format("test: process_received_response/2 - 3.0~n"),
  +    PPRes_Response3 = Test_Response#response{status = 101,
  +					     reason = "Testing"
  +					    },
  +
  +    PRRes_State3 = PRRes_State1#state{sipstate = proceeding,
  +				      response = PPRes_Response3#response{status = 100},
  +				      request  = Test_Request#request{method = "TESTING"}
  +				     },
  +
  +    io:format("test: process_received_response/2 - 3.1~n"),
  +    %% test receiving a non-INVITE response, and at the same time test
  +    %% update_transaction_state/4 without state change
  +    PRRes_State3_out = process_received_response(PPRes_Response3, PRRes_State3),
  +
  +    io:format("test: process_received_response/2 - 3.2~n"),
  +    %% verify new state
  +    siptimer:cancel_all_timers(PRRes_State3_out#state.timerlist),
  +    101 = (PRRes_State3_out#state.response)#response.status,
  +    "Testing" = (PRRes_State3_out#state.response)#response.reason,
  +    [] = siptimer:test_get_appsignals(PRRes_State3_out#state.timerlist),
  +    proceeding = PRRes_State3_out#state.sipstate,
  +
  +    erase({sipsocket_test, is_reliable_transport}),
  +
  +    %% act_on_new_sipstate2(SipState, BranchAction, State)
  +    %%--------------------------------------------------------------------
  +
  +    io:format("test: act_on_new_sipstate2/3 proceeding - 1.1~n"),
  +    %% test do_cancel set to {true, []}
  +    ActOnNewSS_State1 = #state{logtag    = "testing",
  +			       do_cancel = {true, []},
  +			       cancelled = true,	%% to not actually start a CANCEL
  +			       socket    = #sipsocket{proto = yxa_test,
  +						      module = sipsocket_test
  +						     },
  +			       branch    = "branch",
  +			       tl_branch = "tl_branch",
  +			       dst       = Test_Dst,
  +			       request   = Test_Request,
  +			       response  = Test_Response,
  +			       sipstate  = confirmed,
  +			       report_to = self(),
  +			       timerlist = siptimer:empty(),
  +			       testing   = true
  +			      },
  +
  +    {ActOnNewSS_State1_out, ignore} = act_on_new_sipstate2(proceeding, tell_parent, ActOnNewSS_State1),
  +
  +    io:format("test: act_on_new_sipstate2/3 proceeding - 1.2~n"),
  +    %% verify new state
  +    false = ActOnNewSS_State1_out#state.do_cancel,
  +
  +    io:format("test: act_on_new_sipstate2/3 proceeding - 2~n"),
  +    %% test do_cancel set to false
  +    ActOnNewSS_State2 = ActOnNewSS_State1#state{do_cancel = false},
  +    {ActOnNewSS_State2, tell_parent} = act_on_new_sipstate2(proceeding, tell_parent, ActOnNewSS_State2),
  +
  +    io:format("test: act_on_new_sipstate2/3 proceeding - 3~n"),
  +    %% test non-INVITE
  +    ActOnNewSS_State3 = ActOnNewSS_State1#state{do_cancel = false,
  +						request = Test_Request#request{method = "OPTIONS"}
  +					       },
  +    {ActOnNewSS_State3, tell_parent} = act_on_new_sipstate2(proceeding, tell_parent, ActOnNewSS_State3),
  +
  +
  +    io:format("test: act_on_new_sipstate2/3 completed - 4.0~n"),
  +    put({sipsocket_test, is_reliable_transport}, true),
  +    ActOnNewSS_State4 = #state{logtag    = "testing",
  +			       request   = Test_Request,
  +			       sipstate  = confirmed,
  +			       socket    = #sipsocket{proto = yxa_test, module = sipsocket_test},
  +			       timerlist = siptimer:empty()
  +			      },
  +
  +    io:format("test: act_on_new_sipstate2/3 completed - 4.1~n"),
  +    %% test normal case with INVITE and reliable transport
  +    {ActOnNewSS_State4_out, ignore} = act_on_new_sipstate2(completed, ignore, ActOnNewSS_State4),
  +
  +    io:format("test: act_on_new_sipstate2/3 completed - 4.2~n"),
  +    %% verify new state
  +    siptimer:cancel_all_timers(ActOnNewSS_State4_out#state.timerlist),
  +    [{terminate_transaction}] = siptimer:test_get_appsignals(ActOnNewSS_State4_out#state.timerlist),
  +    erase({sipsocket_test, is_reliable_transport}),
  +
  +    io:format("test: act_on_new_sipstate2/3 completed - 5.1~n"),
  +    %% test non-INVITE with unreliable transport
  +    put({sipsocket_test, is_reliable_transport}, false),
  +    ActOnNewSS_State5 = ActOnNewSS_State4#state{request = Test_Request#request{method = "OPTIONS"}},
  +    {ActOnNewSS_State5_out, ignore} = act_on_new_sipstate2(completed, ignore, ActOnNewSS_State5),
  +
  +    io:format("test: act_on_new_sipstate2/3 completed - 5.2~n"),
  +    %% verify new state
  +    siptimer:cancel_all_timers(ActOnNewSS_State5_out#state.timerlist),
  +    [{terminate_transaction}] = siptimer:test_get_appsignals(ActOnNewSS_State5_out#state.timerlist),
  +
  +    io:format("test: act_on_new_sipstate2/3 terminated - 6~n"),
  +    ActOnNewSS_State6 = #state{timerlist = siptimer:empty()},
  +    {ActOnNewSS_State6, ignore} = act_on_new_sipstate2(terminated, ignore, ActOnNewSS_State6),
  +
  +    io:format("test: act_on_new_sipstate2/3 trying - 7~n"),
  +    {#state{}, ignore} = act_on_new_sipstate2(calling, ignore, #state{}),
  +
  +    io:format("test: act_on_new_sipstate2/3 calling - 8~n"),
  +    {#state{}, ignore} = act_on_new_sipstate2(calling, ignore, #state{}),
  +
  +
  +    %% perform_branchaction(BranchAction, State)
  +    %%--------------------------------------------------------------------
  +    io:format("test: perform_branchaction/2 - 1~n"),
  +    %% test final response when parent has already received a final response
  +    PerformBAction_State1 = #state{logtag       = "testing",
  +				   request      = Test_Request,
  +				   response     = {400, "Testing"},
  +				   final_r_sent = true,
  +				   report_to    = self(),
  +				   testing      = true
  +				  },
  +    PerformBAction_State1 = perform_branchaction(tell_parent, PerformBAction_State1),
  +
  +    io:format("test: perform_branchaction/2 - 2~n"),
  +    %% test with undefined report_to
  +    PerformBAction_State2 = PerformBAction_State1#state{report_to = undefined},
  +    PerformBAction_State2 = perform_branchaction(tell_parent, PerformBAction_State2),
  +
  +    io:format("test: perform_branchaction/2 - 3~n"),
  +    %% test with dead report_to
  +    DeadPid = spawn(fun() -> ok end),
  +    erlang:yield(),
  +    PerformBAction_State3 = PerformBAction_State1#state{report_to = DeadPid},
  +    PerformBAction_State3 = perform_branchaction(tell_parent, PerformBAction_State3),
  +
  +    io:format("test: perform_branchaction/2 - 4.1~n"),
  +    %% test non-final response
  +    PerformBAction_State4 = PerformBAction_State1#state{final_r_sent = false,
  +							branch       = "branch",
  +							sipstate     = proceeding,
  +						        response     = {101, "Testing"}
  +						       },
  +    PerformBAction_State4 = perform_branchaction(tell_parent, PerformBAction_State4),
  +
  +    io:format("test: perform_branchaction/2 - 4.2~n"),
  +    %% verify that report_to was notified
  +    receive
  +	{branch_result, Self, "branch", proceeding, {101, "Testing"}} ->
  +	    ok
  +    after 0 ->
  +	    throw("test failed, report_to was not notified about branch result")
  +    end,
  +
  +
  +    %% received_response_state_machine(Method, Status, State)
       %%--------------------------------------------------------------------
   
       %% 17.1.1 INVITE Client Transaction
  
positive.code:     NewState =
  	case siptimer:get_timer(TRef, State#state.timerlist) of
  	    none ->
  		%% This is not really a serious situation. It is actually a race condition that
  		%% shows up from time to time under heavy load - the timer might have fired just
  		%% before we deleted it from our siptimer list.
  		%%logger:log(error, "~s: Unknown timer (~p:~p) fired! Ignoring.", [LogTag, TRef, TDesc]),
  		logger:log(error, "~s: Unknown timer (~p:~p) fired! Ignoring. SIP state '~p', Timerlist :~n~p",
  			   [LogTag, TRef, TDesc, State#state.sipstate, State#state.timerlist]),
  		State;
  	    Timer ->
  
negative.code:     LogTag = State#state.logtag,
      Request = State#state.request,
      URI = Request#request.uri,
      logger:log(debug, "~s: Request 'INVITE ~s' timeout after ~s seconds",
  	       [LogTag, sipurl:print(URI), siptimer:timeout2str(Timeout)]),
      NewState1 =
  	case State#state.final_r_sent of
  	    true ->
  		%% belts and straps, don't overwrite the response in State if
  		%% we have sent a response already, and don't generate an extra
  		%% {branch_result, ...} message for our parent
  		State;
  	    false ->
  		NewState2 = State#state{response = {408, "Request Timeout"}},
  		perform_branchaction(tell_parent, NewState2)
  	end,
      end_invite(NewState1);
  
  %%--------------------------------------------------------------------
  %% Function: process_timer2({invite_expire}, Timer, State)
  