public void ProcessSessionsQueue()
        {
            var clientId = clientsForSession.Take();
            var session  = sessionManager.GetSession(clientId);

            MqttMsgPublish outgoingMsg;

            while (session.OutgoingMessages.TryDequeue(out outgoingMsg))
            {
                var query = from s in session.Subscriptions
                            where new Regex(s.Topic).IsMatch(outgoingMsg.Topic)

                            // check for topics based also on wildcard with regex
                            select s;

                var subscription = query.FirstOrDefault();

                if (subscription != null)
                {
                    var qosLevel = subscription.QosLevel < outgoingMsg.QosLevel
                                       ? subscription.QosLevel
                                       : outgoingMsg.QosLevel;
                    incommingMessageHandler.Publish(
                        subscription.Connection,
                        outgoingMsg.Topic,
                        outgoingMsg.Message,
                        qosLevel,
                        outgoingMsg.Retain);
                }
            }
        }
        public void ProcessSubscribersForRetainedQueue()
        {
            var subscription = SubscribersForRetained.Take();
            var query        = from p in RetainedMessages
                               where new Regex(subscription.Topic).IsMatch(p.Key)
                               select p.Value;

            foreach (var retained in query)
            {
                var qosLevel = subscription.QosLevel < retained.QosLevel ? subscription.QosLevel : retained.QosLevel;

                // send PUBLISH message to the current subscriber
                incommingMessageHandler.Publish(
                    subscription.Connection,
                    retained.Topic,
                    retained.Message,
                    qosLevel,
                    retained.Retain);
            }
        }