anchor.diff:              resend(S, Replay1)
       end.
   
  +maybe_start_timer(S = #state{ keep_alive = 0 }) ->
  +    S;
  +maybe_start_timer(S = #state{socket = Sock, keep_alive = K }) ->
  +    {ok, [{send_oct, Count}]} = inet:getstat(Sock, [send_oct]),
  +    Ref = gen_fsm:start_timer(K * 1000, Count),
  +    S#state{ timer_ref = Ref }.
  +
  +stop_timer(S = #state{ timer_ref = undefined }) ->
  +    S;
  +stop_timer(S = #state{ timer_ref = Ref }) ->
  +    gen_fsm:cancel_timer(Ref),
  +    S#state{ timer_ref = undefined }.
  +
   %% For the oft-repeated {next_state, opened, ...}
   -define(OPENED(S), {next_state, opened, S}).
  +
  +opened({timeout, Ref, Count},
  +       S0 = #state{timer_ref = Ref, socket = Sock }) ->
  +    {ok, [{send_oct, Count1}]} = inet:getstat(Sock, [send_oct]),
  +    case Count1 of
  +        Count ->
  +            ok = write(S0, pingreq);
  +        _ -> ok
  +    end,
  +    ?OPENED(maybe_start_timer(S0));
   
   opened({publish, P0}, S0 = #state{ id_counter = NextId,
                                      replay = Replay }) ->
  
positive.code: init([Address, ClientId, ConnectOpts, Receiver]) ->
      Monitor = monitor(process, Receiver),
      CleanSession = proplists:get_value(clean_session, ConnectOpts, true),
      {ok, unopened, #state{
             connect_args = {Address, ClientId, ConnectOpts},
             clean_session = CleanSession,
             receiver = Receiver,
             receiver_monitor = Monitor
            }}.
  
  %% states
  
negative.code:     S1 = do_rpc(S0, Frame, Ref, From),
      ?OPENED(S1);
  
  opened({frame, pingresp}, S0) -> ?OPENED(S0);
  
  opened({frame, Frame = #publish{}}, S0) ->
      S1 = do_ack(S0, Frame),
      #state{ receiver = Receiver } = S1,
  