anchor.diff:  %% and reopen behaviour by examining watchdog events.
   
   reopen(_) ->
  -    [] = run([[reopen, T, Wd, N, M]
  -              || Wd <- ?WD_TIMERS,       %% watchdog_timer value
  -                 T <- [listen, connect], %% watchdog to test
  +    [] = run([[reopen, T, W, N, M]
  +              || T <- [listen, connect], %% watchdog to test
  +                 W <- ?WD_TIMERS,        %% watchdog_timer value
                    N <- [0,1,2],           %% DWR's to answer before ignoring
                    M <- ['DWR', 'DWA', 'RAA']]). %% how to induce failback
   
  -reopen(Type, Wd, N, M) ->
  -    Server = start_service(),
  -    Client = start_service(),
  -
  -    %% The peer to the transport whose watchdog is tested is given a
  -    %% long watchdog timeout so that it doesn't send DWR of its own.
  -    {Node, Peer} = {{[], Wd}, {[{module, ?MODULE}], ?WD(?PEER_WD)}},
  -
  -    {{LH,LW},{CH,CW}} = case Type of
  -                            listen  -> {Node, Peer};
  -                            connect -> {Peer, Node}
  -                        end,
  -
  -    LO = [{transport_module, diameter_tcp},
  -          {transport_config, LH ++ [{ip, ?ADDR}, {port, 0}]},
  -          {watchdog_timer, LW}],
  -
  -    {ok, LRef} = diameter:add_transport(Server, {listen, LO}),
  -
  +reopen(Test, Wd, N, M) ->
  +    %% Publish a ref ensure the connecting transport is added only
  +    %% once events from the listening transport are subscribed to.
  +    Ref = make_ref(),
  +    [] = run([[reopen, T, Test, Ref, Wd, N, M] || T <- [listen, connect]]).
  +
  +%% reopen/6
  +
  +reopen(Type, Test, Ref, Wd, N, M) ->
  +    {SvcName, TRef} = start(Type, Ref, opts(Type, Test, Ref, Wd)),
  +    reopen(Type, Test, SvcName, TRef, Wd, N, M).
  +
  +%% start/3
  +
  +start(Type, Ref, Opts) ->
  +    Name = hostname(),
  +    true = diameter:subscribe(Name),
  +    ok = diameter:start_service(Name, [{monitor, self()} | ?SERVICE(Name)]),
  +    {ok, TRef} = diameter:add_transport(Name, {Type, Opts}),
  +    true = diameter_reg:add_new({Type, Ref, Name}),
  +    {Name, TRef}.
  +
  +opts(Type, Test, Ref, Wd) ->
  +    {Timer, Counts, Mod} = opts(Type, Test, Wd),
  +    [{transport_module, diameter_tcp},
  +     {transport_config, Mod ++ [{ip, ?ADDR}, {port, 0}] ++ cfg(Type, Ref)},
  +     {watchdog_timer, Timer},
  +     {watchdog_counts, Counts}].
  +
  +cfg(listen, _) ->
  +    [];
  +cfg(connect, Ref) ->
  +    [{{_, _, SvcName}, _Pid}] = diameter_reg:wait({listen, Ref, '_'}),
  +    [[{ref, LRef} | _]] = diameter:service_info(SvcName, transport),
       [LP] = ?util:lport(tcp, LRef, 20),
  -
  -    CO = [{transport_module, diameter_tcp},
  -          {transport_config, CH ++ [{ip, ?ADDR}, {port, 0},
  -                                    {raddr, ?ADDR}, {rport, LP}]},
  -          {watchdog_timer, CW}],
  -
  -    %% Use a temporary process to ensure the connecting transport is
  -    %% added only once events from the listening transport are
  -    %% subscribed to.
  -    Pid = spawn(fun() -> receive _ -> ok end end),
  -
  -    [] = run([[reopen, Type, T, LRef, Pid, Wd, N, M]
  -              || T <- [{listen, Server}, {connect, Client, CO}]]).
  -
  -%% start_service/1
  -
  -start_service() ->
  -    Name = hostname(),
  -    ok = diameter:start_service(Name, [{monitor, self()} | ?SERVICE(Name)]),
  -    Name.
  +    [{raddr, ?ADDR}, {rport, LP}].
  +
  +opts(Type, Type, Wd) ->
  +    {Wd, [], []};
  +opts(_Type, _Test, _Wd) ->
  +    {?WD(?PEER_WD), [{open, 0}], [{module, ?MODULE}]}.
   
   %% reopen/7
   
  -reopen(Type, {listen = T, SvcName}, Ref, Pid, Wd, N, M) ->
  -    diameter:subscribe(SvcName),
  -    Pid ! ok,
  -    recv(Type, T, SvcName, Ref, Wd, N, M);
  -
  -reopen(Type, {connect = T, SvcName, Opts}, _, Pid, Wd, N, M) ->
  -    diameter:subscribe(SvcName),
  -    MRef = erlang:monitor(process, Pid),
  -    receive {'DOWN', MRef, process, _, _} -> ok end,
  -    {ok, Ref} = diameter:add_transport(SvcName, {T, Opts}),
  -    recv(Type, T, SvcName, Ref, Wd, N, M).
  -
  -%% recv/7
  -
   %% The watchdog to be tested.
  -recv(Type, Type, _SvcName, Ref, Wd, N, M) ->
  +reopen(Type, Type, SvcName, Ref, Wd, N, M) ->
  +    ?LOG("node, ~p, ~p", [Type, Ref]),
  +
       %% Connection should come up immediately as a consequence of
       %% starting the watchdog process. In the accepting case this
       %% results in a new watchdog on a transport waiting for a new
       %% connection.
   
  -    ?EVENT({watchdog, Ref, _, {initial, okay}, _}, 2000),
  -    ?EVENT({up, Ref, _, _, #diameter_packet{}}, 0),
  -
  -    %% Low/high watchdog timeouts.
  -    WdL = jitter(Wd, -2000),
  -    WdH = jitter(Wd, 2000),
  +    {initial, okay} = ?WD_EVENT(Ref),
  +    ?EVENT({up, Ref, _, _, #diameter_packet{}}),
   
       %%   OKAY          Timer expires &      Failover()
       %%                 Pending              SetWatchdog()        SUSPECT
  
positive.code:          {'Host-IP-Address', [?ADDR]},
           {'Vendor-Id', 42},
           {'Product-Name', "OTP/diameter"},
           {'Auth-Application-Id', [?DIAMETER_APP_ID_COMMON]},
           {application, [{alias, Name},
                          {dictionary, ?BASE},
                          {module, ?MODULE}]}]).
  
negative.code:     %% back down after either one or two watchdog expiries, depending
      %% on whether or not DWA restored the connection.
  
      {okay, suspect} = ?WD_EVENT(Ref),
      ?EVENT({down, Ref, _, _}),
  
      %%   SUSPECT       Timer expires        CloseConnection()
      %%                                      SetWatchdog()        DOWN
      %%
      %% Non-response brings the connection down after another timeout.
  
      {suspect, down} = ?WD_EVENT(Ref),
  
      R2 = R1 + choose(M == 'DWA', 1, 0),
      A2 = A1,
      [R2,_,_,A2] = wd_counts(SvcName),
  
      %%   DOWN          Timer expires        AttemptOpen()
      %%                                      SetWatchdog()        DOWN
  