anchor.diff:     {ok, State}.
   
   
  -%%
  -%% new socket stream
  -% io_new(SOpt) ->
  -%    #stream{
  -%       send = pstream:new(opts:val(pack, raw, SOpt))
  -%      ,recv = pstream:new(opts:val(pack, raw, SOpt))
  -%      ,ttl  = pair:lookup([timeout, ttl], ?SO_TTL, SOpt)
  -%      ,tth  = pair:lookup([timeout, tth], ?SO_TTH, SOpt)
  -%      ,ts   = os:timestamp()
  -%    }.
  -
  -%%
  -%% socket connected
  -% io_connect(Port, #stream{}=Sock) ->
  -%    {ok, Peer} = inet:peername(Port),
  -%    {ok, Addr} = inet:sockname(Port),
  -%    Sock#stream{peer = Peer, addr = Addr, tss = os:timestamp(), ts = os:timestamp()}.
  -
  -%%
  -%% set hibernate timeout
  -% io_tth(#stream{}=Sock) ->
  -%    Sock#stream{
  -%       tth = tempus:timer(Sock#stream.tth, hibernate)
  -%    }.
  -
  -
  -%%
  -%% recv packet
  -% io_recv(Pckt, Pipe, #stream{}=Sock) ->
  -%    ?DEBUG("knet [tcp] ~p: recv ~p~n~p", [self(), Sock#stream.peer, Pckt]),
  -%    {Msg, Recv} = pstream:decode(Pckt, Sock#stream.recv),
  -%    lists:foreach(fun(X) -> pipe:b(Pipe, {tcp, self(), X}) end, Msg),
  -%    {active, Sock#stream{recv=Recv}}.
  -
  -%%
  -%% send packet
  -% io_send(Msg, Pipe, #stream{}=Sock) ->
  -%    ?DEBUG("knet [tcp] ~p: send ~p~n~p", [self(), Sock#stream.peer, Msg]),
  -%    {Pckt, Send} = pstream:encode(Msg, Sock#stream.send),
  -%    lists:foreach(fun(X) -> ok = gen_tcp:send(Pipe, X) end, Pckt),
  -%    {active, Sock#stream{send=Send}}.
  -
  -%%
  -%% set socket i/o control flags
  -% tcp_ioctl(#fsm{sock = Sock, active = true} = State) ->
  -%    inet:setopts(Sock, [{active, ?CONFIG_IO_CREDIT}]),
  -%    State;
  -% tcp_ioctl(#fsm{sock = Sock, active = once} = State) ->
  -%    inet:setopts(Sock, [{active, ?CONFIG_IO_CREDIT}]),
  -%    State;
  -% tcp_ioctl(#fsm{sock = Sock, active = N} = State) ->
  -%    inet:setopts(Sock, [{active, N}]),
  -%    State.
  -
  -
  -%%
  -%%
  -% tcp_connect(Uri, #fsm{so = SOpt0}) ->
  -%    Host = scalar:c(uri:host(Uri)),
  -%    Port = uri:port(Uri),
  -%    SOpt = opts:filter(?SO_TCP_ALLOWED, SOpt0),
  -%    Tout = pair:lookup([timeout, ttc], ?SO_TIMEOUT, SOpt0),
  -%    gen_tcp:connect(Host, Port, SOpt, Tout).
  -
  -%%
  -%%
  -% tcp_accept(_Uri, #fsm{so = SOpt0}) ->
  -%    LPipe = opts:val(listen, SOpt0),
  -%    LSock = pipe:ioctl(LPipe, socket),
  -%    gen_tcp:accept(LSock).
  -
  -
  -
  
positive.code: 
  %%
  %% socket timeout
  tcp_ttl(#state{so = SOpt} = State) ->
     [$? ||
        lens:get(lens:pair(timeout, []), lens:pair(ttl, undefined), SOpt),
        tempus:timer(_, ttl)
     ],
     {ok, State}.
  
  tcp_tth(#state{so = SOpt} = State) ->
     [$? ||
        lens:get(lens:pair(timeout, []), lens:pair(tth, undefined), SOpt),
        tempus:timer(_, tth)
     ],
     {ok, State}.
  
  tcp_ttp(#state{socket = Sock, so = SOpt} = State) ->
     [$? ||
        lens:get(lens:pair(timeout, []), lens:pair(ttp, undefined), SOpt),
        tempus:timer(_, {ttp, erlang:element(2, getstat(Sock))})
     ],
     {ok, State}.
  
  tcp_ttp(N, #state{socket = Sock} = State) ->
     case getstat(Sock) of
        {ok, X} when X > N ->
           tcp_ttp(State);
        _ ->
           {error, timeout}
     end.
  
  
  %%
  %% socket up/down link i/o
  tcp_flow_ctrl(#state{flowctl = true, socket = Sock} = State) ->
     [$^ ||
        setopts(Sock, [{active, ?CONFIG_IO_CREDIT}]),
        fmap(State)
     ];
  tcp_flow_ctrl(#state{flowctl = once, socket = Sock} = State) ->
     [$^ ||
        setopts(Sock, [{active, ?CONFIG_IO_CREDIT}]),
        fmap(State)
     ];
  tcp_flow_ctrl(#state{flowctl = _N, socket = Sock} = State) ->
     [$^ ||
        setopts(Sock, [{active, ?CONFIG_IO_CREDIT}]),
        fmap(State)
     ].
  
  %%
  tcp_uplink_established(Pipe, #state{socket = Sock} = State) ->
  
negative.code:    socket   = undefined :: #socket{}
    ,flowctl  = true      :: once | false | true | integer()  %% flow control strategy
    ,trace    = undefined :: _
    ,timeout  = undefined :: [_]
    ,so       = undefined :: [_]
  }).
  
  