anchor.diff:      ?WARN("drain_id=~p channel_id=~p dest=~s err=unexpected_reconnect "
             "expected=~p received=~p state=disconnected",
             log_info(State, [Expected, Received])),
  -    {next_state, disconnected, State};
  +    reconnect(State);
   disconnected({post, Msg}, State) ->
  -    {next_state, disconnected,
  -     reconnect(buffer(Msg, State))};
  +    reconnect(buffer(Msg, State));
   disconnected(Msg, State) ->
       ?WARN("drain_id=~p channel_id=~p dest=~s err=unexpected_info "
             "data=~p state=disconnected",
             log_info(State, [Msg])),
  -    {next_state, disconnected, State}.
  +    reconnect(State).
   
   %% @doc We have a socket open and messages to send. Collect up an
   %% appropriate amount and flush them to the socket.
  -ready_to_send({post, Msg}, State) ->
  +ready_to_send({timeout, _Ref, ?SEND_TIMEOUT_MSG},
  +              State = #state{sock = Sock})
  +  when is_port(Sock) ->
  +    %% Stale message.
  +    send(State);
  +ready_to_send({post, Msg}, State = #state{sock = Sock})
  +  when is_port(Sock) ->
       send(buffer(Msg, State));
  -ready_to_send({inet_reply, Sock, ok}, S = #state{sock = Sock}) ->
  +ready_to_send({inet_reply, Sock, ok}, S = #state{sock = Sock})
  +  when is_port(Sock) ->
       %% Stale inet reply
       send(S);
  -ready_to_send(Msg, State) ->
  +ready_to_send(Msg, State = #state{sock = Sock})
  +  when is_port(Sock) ->
       ?WARN("drain_id=~p channel_id=~p dest=~s err=unexpected_info "
             "data=~p state=ready_to_send",
             log_info(State, [Msg])),
  -    {next_state, disconnected, State}.
  +    reconnect(State).
   
   
   %% @doc We sent some data to the socket and are waiting the result of
   %% the send operation.
  +sending({timeout, Ref, ?SEND_TIMEOUT_MSG},
  +        S = #state{send_tref=Ref}) ->
  +    reconnect(tcp_bad(S#state{send_tref=undefined}));
   sending({post, Msg}, State) ->
       {next_state, sending, buffer(Msg, State)};
   sending({inet_reply, Sock, ok}, S = #state{sock = Sock}) ->
  -    send(S);
  +    send(tcp_good(S));
   sending({inet_reply, Sock, {error, Reason}}, S = #state{sock = Sock}) ->
       ?ERR("drain_id=~p channel_id=~p dest=~s state=~p "
            "err=gen_tcp data=~p sock=~p duration=~s state=sending",
             log_info(S, [sending, Reason, Sock, duration(S)])),
  -    {next_state, disconnected, reconnect(tcp_bad(S))};
  +    reconnect(tcp_bad(S));
   sending(Msg, State) ->
       ?WARN("drain_id=~p channel_id=~p dest=~s err=unexpected_info "
             "data=~p state=sending",
             log_info(State, [Msg])),
  -    {next_state, disconnected, State}.
  +    {next_state, sending, State}.
   
   
   %% @private
  
positive.code: 
  %% @private
  %% @doc Send buffered messages.
  send(State = #state{buf = Buf, sock = Sock,
                      drain_tok = DrainTok}) ->
      case logplex_drain_buffer:empty(Buf) of
  
negative.code:                   MaxExp when F > MaxExp -> Max;
                    _ -> 1 bsl F
                end,
      {next_state, disconnected,
       reconnect_in(timer:seconds(BackOff), State)}.
  
  reconnect_in(MS, State = #state{}) ->
      Ref = erlang:start_timer(MS, self(), ?RECONNECT_MSG),
  