anchor.diff:      tc_try(api_to_connect_tcp4,
              fun() ->
                      ?TT(?SECS(10)),
  -                   InitState = #{domain => inet, timeout => 5000},
  +                   InitState = #{domain        => inet,
  +                                 backlog       => 1,
  +                                 timeout       => 5000,
  +                                 connect_limit => 3},
                      ok = api_to_connect_tcp(InitState)
              end).
   
  
positive.code:          %% *** Wait for start order part ***
           #{desc => "await start (from tester)",
             cmd  => fun(State) ->
                             Tester = ?SEV_AWAIT_START(),
                             {ok, State#{tester => Tester}}
                     end},
           #{desc => "monitor tester",
             cmd  => fun(#{tester := Tester} = _State) ->
  
negative.code:                            ServerSA = LSA#{port => Port},
                             {ok, State#{server_sa => ServerSA}}
                     end},
           #{desc => "order client start",
             cmd  => fun(#{client    := Client,
                           server_sa := ServerSA}) ->
                             ?SEV_ANNOUNCE_START(Client, ServerSA),
                             ok
                     end},
           #{desc => "await client ready (init)",
             cmd  => fun(#{client := Client} = _State) ->
                             ?SEV_AWAIT_READY(Client, client, init),
                             ok
                     end},
  
           %% The actual test
           %% The server does nothing (this is the point), no accept,
           %% the client tries to connect.
           #{desc => "order client continue (connect)",
             cmd  => fun(#{client        := Client,
                           timeout       := Timeout,
                           connect_limit := ConLimit} = _State) ->
                             ?SEV_ANNOUNCE_CONTINUE(Client, connect,
                                                    {Timeout, ConLimit}),
                             ok
                     end},
           #{desc => "await client ready (connect)",
             cmd  => fun(#{server := Server,
                           client := Client} = _State) ->
                             case ?SEV_AWAIT_READY(Client, client, connect,
                                                   [{server, Server}]) of
                                 {ok, _} ->
                                     ok;
                                 {error, _} = ERROR ->
                                     ERROR
                             end
                     end},
  
  
           %% *** Terminate server ***
           #{desc => "order client terminate",
             cmd  => fun(#{client := Client} = _State) ->
                             ?SEV_ANNOUNCE_TERMINATE(Client),
                             ok
                     end},
           #{desc => "await client down",
             cmd  => fun(#{client := Client} = State) ->
                             ?SEV_AWAIT_TERMINATION(Client),
                             State1 = maps:remove(client,    State),
                             {ok, State1}
                     end},
           #{desc => "order server terminate",
             cmd  => fun(#{server := Server} = _State) ->
                             ?SEV_ANNOUNCE_TERMINATE(Server),
                             ok
                     end},
           #{desc => "await server down",
             cmd  => fun(#{server := Server} = State) ->
                             ?SEV_AWAIT_TERMINATION(Server),
                             State1 = maps:remove(server,    State),
                             State2 = maps:remove(server_sa, State1),
                             {ok, State2}
                     end},
  
           %% *** We are done ***
           ?SEV_FINISH_NORMAL
          ],
  
      i("create server evaluator"),
      ServerInitState = #{domain => maps:get(domain, InitState)},
      Server          = ?SEV_START("server", ServerSeq, ServerInitState),
  
      i("create client evaluator"),
      ClientInitState = #{host   => local_host(),
                          domain => maps:get(domain, InitState)},
      Client          = ?SEV_START("client", ClientSeq, ClientInitState),
  
      i("create tester evaluator"),
      TesterInitState = InitState#{server => Server#ev.pid,
                                   client => Client#ev.pid},
      Tester          = ?SEV_START("tester", TesterSeq, TesterInitState),
  
      i("await evaluator(s)"),
      ok = ?SEV_AWAIT_FINISH([Server, Client, Tester]).
  
  
  api_toc_tcp_client_start(Node) ->
      Self = self(),
      GL   = group_leader(),
      Fun  = fun() -> api_toc_tcp_client(Self, GL) end,
      erlang:spawn(Node, Fun).
  
  api_toc_tcp_client(Parent, GL) ->
      api_toc_tcp_client_init(Parent, GL),
      ServerSA = api_toc_tcp_client_await_start(Parent),
      Domain   = maps:get(family, ServerSA),
      api_toc_tcp_client_announce_ready(Parent, init),
      {To, ConLimit} = api_toc_tcp_client_await_continue(Parent, connect),
      Result = api_to_connect_tcp_await_timeout(To, ServerSA, Domain, ConLimit),
      api_toc_tcp_client_announce_ready(Parent, connect, Result),
      Reason = sc_rs_tcp_client_await_terminate(Parent),
      exit(Reason).
  
  api_toc_tcp_client_init(Parent, GL) ->
      %% i("api_toc_tcp_client_init -> entry"),
      _MRef = erlang:monitor(process, Parent),
      group_leader(self(), GL),
      ok.
  
  api_toc_tcp_client_await_start(Parent) ->
      %% i("api_toc_tcp_client_await_start -> entry"),
      ?SEV_AWAIT_START(Parent).
  
  api_toc_tcp_client_announce_ready(Parent, Slogan) ->
      ?SEV_ANNOUNCE_READY(Parent, Slogan).
  api_toc_tcp_client_announce_ready(Parent, Slogan, Result) ->
      ?SEV_ANNOUNCE_READY(Parent, Slogan, Result).
  
  api_toc_tcp_client_await_continue(Parent, Slogan) ->
      %% i("api_toc_tcp_client_await_continue -> entry"),
      case ?SEV_AWAIT_CONTINUE(Parent, parent, Slogan) of
          ok ->
              ok;
          {ok, Extra} ->
              Extra;
          {error, Reason} ->
              exit({await_continue, Slogan, Reason})
      end.
  
  api_toc_tcp_client_await_terminate(Parent) ->
      %% i("api_toc_tcp_client_await_terminate -> entry"),
      case ?SEV_AWAIT_TERMINATE(Parent, parent) of
          ok ->
              ok;
          {error, Reason} ->
              Reason
      end.
  
  api_to_connect_tcp_await_timeout(To, ServerSA, Domain, ConLimit) ->
      LAddr = which_local_addr(Domain),
      LSA   = #{family => Domain,
                addr   => LAddr},
      NewSock = fun() ->
                        S = case socket:open(Domain, stream, tcp) of
                                {ok, Sock} ->
                                    Sock;
                                {error, OReason} ->
                                    ?FAIL({open, OReason})
                            end,
                        case socket:bind(S, LSA) of
                            {ok, _} ->
                                S;
                            {error, BReason} ->
                                ?FAIL({bind, BReason})
                        end
                end,
      api_to_connect_tcp_await_timeout(1, ConLimit, To, ServerSA, NewSock, []).
  
  api_to_connect_tcp_await_timeout(ID, ConLimit, _To, _ServerSA, _NewSock, Acc)
    when (ID > ConLimit) ->
      api_to_connect_tcp_await_timeout3(Acc),
      {error, {connect_limit_reached, ID, ConLimit}};
  api_to_connect_tcp_await_timeout(ID, ConLimit, To, ServerSA, NewSock, Acc) ->
      case api_to_connect_tcp_await_timeout2(ID, To, ServerSA, NewSock) of
          ok ->
              %% ?SEV_IPRINT("success when number of socks: ~w", [length(Acc)]),
              api_to_connect_tcp_await_timeout3(Acc),
              ok;
          {ok, Sock} ->
              %% ?SEV_IPRINT("~w: unexpected success (connect)", [ID]),
              api_to_connect_tcp_await_timeout(ID+1, ConLimit,
                                               To, ServerSA, NewSock,
                                               [Sock|Acc]);
          {error, _} = ERROR ->
              ERROR
      end.
  
  api_to_connect_tcp_await_timeout2(ID, To, ServerSA, NewSock) ->
      Sock = NewSock(),
      %% ?SEV_IPRINT("~w: try connect", [ID]),
      Start = t(),
      case socket:connect(Sock, ServerSA, To) of
          {error, timeout} ->
              Stop  = t(),
              TDiff = tdiff(Start, Stop),
              if
                  (TDiff >= To) ->
                      (catch socket:close(Sock)),
                      ok;
                  true ->
                      (catch socket:close(Sock)),
                      ?FAIL({unexpected_timeout, TDiff, To})
              end;
          {error, econnreset = _Reason} ->
              (catch socket:close(Sock)),
              ok;
          {error, Reason} ->
              (catch socket:close(Sock)),
              ?FAIL({connect, Reason});
          ok ->
              {ok, Sock}
      end.
  
  api_to_connect_tcp_await_timeout3([]) ->
      ok;
  api_to_connect_tcp_await_timeout3([Sock|Socka]) ->
      (catch socket:close(Sock)),
      api_to_connect_tcp_await_timeout3(Socka).
  
  %% api_to_connect_tcp_await_timeout(Socks, To, ServerSA) ->
  %%     api_to_connect_tcp_await_timeout(Socks, To, ServerSA, 1).
  
  %% api_to_connect_tcp_await_timeout([], _To, _ServerSA, _ID) ->
  %%     ?FAIL(unexpected_success);
  %% api_to_connect_tcp_await_timeout([Sock|Socks], To, ServerSA, ID) ->
  %%     ?SEV_IPRINT("~w: try connect", [ID]),
  %%     Start = t(),
  %%     case socket:connect(Sock, ServerSA, To) of
  %%         {error, timeout} ->
  %%             ?SEV_IPRINT("expected timeout (~w)", [ID]),
  %%             Stop  = t(),
  %%             TDiff = tdiff(Start, Stop),
  %%             if
  %%                 (TDiff >= To) ->
  %%                     ok;
  %%                 true ->
  %%                     {error, {unexpected_timeout, TDiff, To}}
  %%             end;
  %%         {error, econnreset = Reason} ->
  %%             ?SEV_IPRINT("failed connecting: ~p - giving up", [Reason]),
  %%             ok;
  %%         {error, Reason} ->
  %%             ?SEV_EPRINT("failed connecting: ~p", [Reason]),
  %%             ?FAIL({connect, Reason});
  %%         ok ->
  %%             ?SEV_IPRINT("unexpected success (~w) - try next", [ID]),
  %%             api_to_connect_tcp_await_timeout(Socks, To, ServerSA, ID+1)
  %%     end.
          
  
  
  