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: connect(#state{}) ->
      {error, bogus_port_number}.
  
  -spec reconnect(#state{}) -> #state{}.
  %% @private
  reconnect(State = #state{reconnect_tref = Ref}) when is_reference(Ref) ->
      %% Reconnect timer was set
  
negative.code:         not_empty ->
              {Data, N, NewBuf} =
                  buffer_to_pkts(Buf, ?TARGET_SEND_SIZE, DrainTok),
              Ref = erlang:start_timer(?SEND_TIMEOUT, self(), ?SEND_TIMEOUT_MSG),
              erlang:port_command(Sock, Data, []),
              msg_stat(drain_delivered, N, State),
              {next_state, sending, State#state{buf = NewBuf,
                                                send_tref=Ref}}
      end.
  
  buffer_to_pkts(Buf, BytesRemaining, DrainTok)
  