anchor.diff:    when is_port(Sock) ->
       %% Stale message.
       send(State);
  -ready_to_send({timeout, _Ref, ?IDLE_TIMEOUT_MSG},
  -              State = #state{sock = Sock}) ->
  -    ?INFO("drain_id=~p channel_id=~p dest=~s at=idle_timeout "
  -          "state=ready_to_send",
  -          log_info(State, [])),
  -    gen_tcp:close(Sock),
  -    {next_state, disconnected, State#state{sock = undefined,
  -                                           idle_tref = undefined}, hibernate};
  +ready_to_send({timeout, _Ref, ?IDLE_TIMEOUT_MSG}, S) ->
  +    case close_if_idle(S) of
  +        closed ->
  +            {next_state, disconnected, S#state{sock = undefined}, hibernate};
  +        ok ->
  +            {next_state, ready_to_send, S}
  +    end;
   ready_to_send({post, Msg}, State = #state{sock = Sock})
     when is_port(Sock) ->
       send(buffer(Msg, State));
  
positive.code: 
  -spec buffer(any(), #state{}) -> #state{}.
  %% @private
  buffer(Msg, State = #state{buf = Buf, idle_tref = TRef}) ->
      cancel_timeout(TRef, ?IDLE_TIMEOUT_MSG),
      TimeoutMs = logplex_app:config(tcp_syslog_idle_timeout, timer:minutes(5)),
      Ref = erlang:start_timer(TimeoutMs, self(), ?IDLE_TIMEOUT_MSG),
      {Result, NewBuf} = logplex_msg_buffer:push_ext(Msg, Buf),
      msg_stat(drain_buffered, 1, State),
      case Result of
  
negative.code:             undefined
        end.
  
  close_if_idle(#state{sock = Sock, last_good_time = LastGood}) ->
      MaxIdle = logplex_app:config(tcp_syslog_idle_timeout, timer:minutes(5)),
      {DayDiff, TimeDiff} = calendar:time_difference(os:timestamp(), LastGood),
      %% technically time_to_seconds is obsolete, but not in a way that matters
      SinceLastGood = calendar:time_to_seconds(TimeDiff) * 1000,
      case {SinceLastGood > MaxIdle, DayDiff} of
          {true, 0} ->
              gen_tcp:close(Sock),
              closed;
          _ ->
              erlang:start_timer(MaxIdle, self(), ?IDLE_TIMEOUT_MSG),
              ok
      end.
  
  buffer_to_pkts(Buf, BytesRemaining, DrainTok) ->
      logplex_msg_buffer:to_pkts(Buf, BytesRemaining,
                                 pkt_fmt(DrainTok)).
  