anchor.diff:             {ok, NewSessionState, serialise([Out])}
      end.
   
  -in(Msg, {StateFun, State}) ->
  -    StateFun(Msg, State).
  +%%% init  --> | wait_for_connect | --> | connected | --> terminate
  +in(Msg, {connected, State}) ->
  +    case connected(Msg, State) of
  +        {stop, _, _} = R -> R;
  +        {NewState, Out} ->
  +            {{connected, set_last_time_active(NewState)}, Out}
  +    end;
  +in(Msg, {wait_for_connect, State}) ->
  +    case wait_for_connect(Msg, State) of
  +        {stop, _, _} = R -> R;
  +        {NewState, Out} ->
  +            %% state transition to | connected |
  +            {{connected, set_last_time_active(NewState)}, Out}
  +    end.
  +
   
   send(SessionPid, Msg) ->
       SessionPid ! {?MODULE, Msg},
  
positive.code:     UpgradeQoS = vmq_config:get_env(upgrade_outgoing_qos, false),
      RegView = vmq_config:get_env(default_reg_view, vmq_reg_trie),
      TRef = send_after(?CLOSE_AFTER, close_timeout),
      {fun wait_for_connect/2, #state{peer=Peer,
                                       upgrade_qos=UpgradeQoS,
                                       mountpoint=string:strip(MountPoint, right, $/),
                                       allow_anonymous=AllowAnonymous,
  
negative.code: serialise([[F|T]|Frames], Acc) ->
      serialise([T|Frames], [vmq_parser:serialise(F)|Acc]).
  
  -spec wait_for_connect(mqtt_frame(), state()) ->
      {state(), [mqtt_frame() | binary()]} | {stop, any(), [mqtt_frame() | binary()]}.
  wait_for_connect(#mqtt_connect{keep_alive=KeepAlive} = Frame,
                   #state{keep_alive_tref=TRef} = State) ->
      cancel_timer(TRef),
      _ = vmq_exo:incr_connect_received(),
      %% the client is allowed "grace" of a half a time period
      KKeepAlive = (KeepAlive + (KeepAlive div 2)) * 1000,
      set_keepalive_timer(KKeepAlive),
      check_connect(Frame, incr_msg_recv_cnt(
                             State#state{keep_alive=KKeepAlive,
                                         keep_alive_tref=undefined}));
  