anchor.diff:      end.
   
   
  -client_start(Parent, Backend, ID,
  -	     LifeTime,
  -	     Domain, Type, Proto,
  -	     ServerAddr, ServerPort)
  +start_esock_client(#{domain   := Domain,
  +		     type     := Type,
  +		     protocol := Proto,
  +		     server   := #{addr := Addr, port := Port},
  +		     cid      := ID,
  +		     clients  := Clients0} = State) ->
  +    Self = self(),
  +    i("try start (esock) client ~w", [ID]),
  +    LifeTime = rand:uniform(timer:minutes(3)) + timer:minutes(2),
  +    {Pid, MRef} =
  +	spawn_monitor(fun() ->
  +			      esock_client_start(Self,
  +						 ID,
  +						 LifeTime,
  +						 Domain, Type, Proto,
  +						 Addr, Port)
  +		      end),
  +    receive
  +	{'DOWN', MRef, process, Pid, Reason} ->
  +	    e("received unexpected down message from client ~w (~p):"
  +	      "~n   ~p", [ID, Pid, Reason]),
  +	    client_ctrl_exit({client, ID}, Reason);
  +
  +	{Pid, ready} ->
  +	    i("received expected ready from client ~w (~p)", [ID, Pid]),
  +	    Clients1 = Clients0#{Pid => {ID, MRef}},
  +	    State#{cid => ID + 1, clients => Clients1}
  +    end.
  +
  +
  +gen_client_start(Parent, Backend, ID,
  +		 LifeTime,
  +		 Domain, Type, Proto,
  +		 ServerAddr, ServerPort)
     when (Type =:= stream) andalso (Proto =:= tcp) ->
  -    put(sname, f("client[~w]", [ID])),
  +    put(sname, f("gen-client[~w,~w]", [Backend, ID])),
       %% put(debug, true),
       i("starting"),
  +    State = gen_client_connect(#{id          => ID,
  +				 backend     => Backend,
  +				 domain      => Domain,
  +				 type        => Type,
  +				 protocol    => Proto,
  +				 server_addr => ServerAddr,
  +				 server_port => ServerPort}),
       erlang:send_after(LifeTime, self(), terminate),
  -    State = client_connect(#{id          => ID,
  -			     backend     => Backend,
  -			     domain      => Domain,
  -			     type        => Type,
  -			     protocol    => Proto,
  -			     server_addr => ServerAddr,
  -			     server_port => ServerPort}),
       MRef = erlang:monitor(process, Parent),
       Parent ! {self(), ready},
       i("started"),
  -    client_loop(State#{condition => send_request,
  -		       mid       => 1,
  -		       parent    => Parent,
  -		       mref      => MRef,
  -		       buf       => <<>>}).
  -
  -
  -client_connect(#{backend     := Backend,
  -		 server_addr := ServerAddr,
  -		 server_port := ServerPort} = State) ->
  +    gen_client_loop(State#{condition => send_request,
  +			   mid       => 1,
  +			   parent    => Parent,
  +			   mref      => MRef,
  +			   buf       => <<>>}).
  +
  +
  +gen_client_connect(#{backend     := Backend,
  +		     type        := Type,
  +		     protocol    := Proto,
  +		     server_addr := ServerAddr,
  +		     server_port := ServerPort} = State)
  +  when (Type =:= stream) andalso (Proto =:= tcp) ->
       COpts = [{inet_backend, Backend}, {active, true}, binary],
  -    i("[~w] try connect to ~s:~w",
  -      [Backend, inet_parse:ntoa(ServerAddr), ServerPort]),
  +    i("try connect to ~s:~w",
  +      [inet_parse:ntoa(ServerAddr), ServerPort]),
       case gen_tcp:connect(ServerAddr, ServerPort, COpts) of
   	{ok, Sock} ->
  -	    %% i("[~w] connected", [Backend]),
  +	    %% i("connected"),
   	    ?SOCKET_MONITOR !
   		{monitor, Sock,
   		 string_of(inet, Sock),
  
positive.code:     case socket:recv(Sock) of
  	{ok, Data} ->
  	    handler_process_data(State, Data);
  	{select, SelectInfo} ->
  	    State#{select => SelectInfo};
  	{error, closed} ->
  	    i("recv got closed => client done"),
  
negative.code: 			MRef, Pid, Reason) ->
      case maps:take(Pid, Clients0) of
  	{{ID, MRef, Backend}, Clients1} ->
  	    i("received down from (gen) ~w-client ~w (~p): "
  	      "~n   ~p", [Backend, ID, Pid, Reason]),
  	    start_gen_client(State#{clients => Clients1}, Backend);
  	{{ID, MRef}, Clients1} ->
  	    i("received down from (esock) ~w-client ~w (~p): "
  	      "~n   ~p", [ID, Pid, Reason]),
  	    start_esock_client(State#{clients => Clients1});
  	error ->
  	    i("received down from unknown process ~p: "
  	      "~n   ~p", [Pid, Reason]),
  