anchor.diff:      S#state{session_in = publish(Packet,S)}.
   
   publish(Packet = #'PUBLISH'{packet_id = PacketId,
  -                            qos       = QoS},
  +                            qos = QoS},
           #state{client_id     = ClientId,
                  session_in    = SessionIn,
                  sender_pid    = SenderPid}) ->
  
positive.code: 
  
  %% Valid complete packet!
  handle_packet(#'CONNECT'{client_id = ClientId,keep_alive = KeepAliveTimeout,
                           clean_session = CleanSession,will = Will,
                           password = Password,username = Username},
                S = #state{connect_state = connecting,
                           security = {Security,SecConf}}) ->
  
      %%=======================================================================
      %% @todo: validate connect packet
      %%=======================================================================
  
      %%=======================================================================
      %% @todo: authorize Will?!?!?
      %%=======================================================================
  
      case Security:authenticate(SecConf,ClientId,Username,Password) of
          {error,Reason} ->
              Code = case Reason of
                         bad_credentials -> ?CONNACK_BAD_USERNAME_OR_PASSWORD;
                         _ -> ?CONNACK_UNAUTHORIZED
                     end,
              send_to_client(S,#'CONNACK'{session_present = false,return_code = Code}),
              prevent_connection(S,bad_auth);
          {ok, AuthCtx} ->
              S1 = S#state{auth_ctx = AuthCtx},
              %%=======================================================================
              %% @todo: determine session state
              %%=======================================================================
  %% 			SessionPresent = if(CleanSession) ->
  %% 				mqtt_session_repo:clear(ClientId);
  %% 				                 false,
  %% 				                 true ->
  %% 					                 true  %% @todo: determine session state
  %% 			                 end,
  
              %register_self(ClientId,CleanSession),
  
              SessionPresent =
              case CleanSession of
                  false  -> true;%%error({not_supported,persistent_session});
                  true   -> false
              end,
              S2 = S1#state{clean_session = CleanSession,
                            client_id = ClientId,
                            session_in = mqtt_publish:new(ClientId,Will),
                            session_out = new_session(S1,ClientId,CleanSession)},
  
              %% @todo:  Determine session present
              send_to_client(S2, #'CONNACK'{return_code = ?CONNACK_ACCEPTED,
                                            session_present = SessionPresent}),
  
              S4 = maybe_start_keep_alive(S2, KeepAliveTimeout * 1000),
              S5 = S4#state{connect_state = connected},
              {noreply,S5}
      end;
  
  %% Catch- all case
  handle_packet(Packet, S = #state{ connect_state = connecting})
  
negative.code: 
  
  %% =================================================
  %% Connect
  %% =================================================
  
  authorize(#'CONNECT'{client_id = ClientId,
                       password = Password,
                       username = Username},
            S = #state{security = {Security,SecConf}}) ->
      %%=======================================================================
      %% @todo: authorize Will?!?!?
      %%=======================================================================
      case Security:authenticate(SecConf,ClientId,Username,Password) of
          {error,Reason} ->
              Code = case Reason of
                         bad_credentials -> ?CONNACK_BAD_USERNAME_OR_PASSWORD;
                         _               -> ?CONNACK_UNAUTHORIZED
                     end,
              send_to_client(S,#'CONNACK'{session_present = false,return_code = Code}),
              throw({bad_auth,S});
          {ok, AuthCtx} ->
              S#state{auth_ctx = AuthCtx}
      end.
  
  establish_session(#'CONNECT'{client_id = ClientId,
                               clean_session = CleanSession,
                               will = Will},
                    S) ->
      SessionPresent = not CleanSession,
      NewState = S#state{clean_session = CleanSession,
                         client_id = ClientId,
                         session_in = mqtt_publish:new(ClientId,Will),
                         session_out = new_session(S,ClientId,CleanSession)},
  
      %% @todo:  Determine session present
      send_to_client(NewState, #'CONNACK'{return_code = ?CONNACK_ACCEPTED,
                                          session_present = SessionPresent}),
      NewState.
  
  %% =================================================
  %% Publish
  %% =================================================
  
  