public async Task ExecuteAsync(string clientId, IPacket input, IMqttChannel <IPacket> channel) { if (input.Type != MqttPacketType.Connect) { return; } Connect connect = input as Connect; if (!_authenticationProvider.Authenticate(clientId, connect.UserName, connect.Password)) { throw new MqttConnectionException(MqttConnectionStatus.BadUserNameOrPassword); } ClientSession session = _sessionRepository.Read(clientId); bool sessionPresent = connect.CleanSession ? false : session != null; _tracer.Info($"Client connecting with protocol level {connect.ProtocolLelvel}."); if (connect.CleanSession && session != null) { _sessionRepository.Delete(session.Id); session = null; _tracer.Info(ServerProperties.Server_CleanedOldSession(clientId)); } bool sendPendingMessages = false; if (session == null) { session = new ClientSession(clientId, connect.CleanSession); _sessionRepository.Create(session); _tracer.Info(ServerProperties.Server_CreatedSession(clientId)); } else { sendPendingMessages = true; } if (connect.Will != null) { ConnectionWill connectionWill = new ConnectionWill(clientId, connect.Will); _willRepository.Create(connectionWill); } await channel.SendAsync(new ConnectAck( MqttConnectionStatus.Accepted, sessionPresent )); if (sendPendingMessages) { await SendPendingMessagesAsync(session, channel); await SendPendingAcknowledgementsAsync(session, channel); } }