private void ProcOldMessages(CallContext cntx, SignalRMessageServiceProxy msgsvc)
        {
            var hsvc = new SignalRHostStateServiceProxy();
            var host = hsvc.LoadEntityByNature(cntx, config.HostName, config.App.ID).SingleOrDefault();

            if (host == null)
            {
                host = new SignalRHostState
                {
                    HostName      = config.HostName,
                    ApplicationID = config.App.ID,
                    LastMsgId     = 0
                };
                var x = hsvc.AddOrUpdateEntities(cntx, new SignalRHostStateSet(), new SignalRHostState[] { host });
                host = x.ChangedEntities[0].UpdatedItem;
            }
            DateTime       dt    = DateTime.UtcNow.AddHours(-config.TimeWindowInHours);
            QueryExpresion qexpr = new QueryExpresion();

            qexpr.OrderTks = new List <QToken>(new QToken[] {
                new QToken {
                    TkName = "ID"
                },
                new QToken {
                    TkName = "desc"
                }
            });
            qexpr.FilterTks = new List <QToken>(new QToken[] {
                new QToken {
                    TkName = "ApplicationID == \"" + config.App.ID + "\" && ID > " + host.LastMsgId + " && TimeStamp > " + dt.Ticks
                }
            });
            var msgs = msgsvc.QueryDatabaseLimited(cntx, new SignalRMessageSet(), qexpr, config.MaxBacklogMessages).ToArray();

            if (msgs.Length > 0)
            {
                host.LastMsgId         = msgs[0].ID;
                LastMessageId          = (ulong)host.LastMsgId;
                IsLastMessageIdChanged = true;
                foreach (var e in from d in msgs orderby d.ID ascending select d)
                {
                    OnReceived(0, (ulong)e.ID, ScaleoutMessage.FromBytes(e.MesssageData));
                }
            }
        }
        private static void LastIdUpdateThread()
        {
            var hsvc = new SignalRHostStateServiceProxy();
            SignalRHostStateSet set = new SignalRHostStateSet();

            try
            {
                evt.Set();
                while (true)
                {
                    Thread.Sleep(1000 * config.HostStateUpdateIntervalInSeconds);
                    if (IsLastMessageIdChanged)
                    {
                        SignalRHostState host = new SignalRHostState
                        {
                            IsPersisted         = true,
                            HostName            = config.HostName,
                            ApplicationID       = config.App.ID,
                            LastMsgId           = (long)LastMessageId,
                            IsLastMsgIdModified = true
                        };
                        try
                        {
                            hsvc.AddOrUpdateEntities(Cntx, set, new SignalRHostState[] { host });
                            IsLastMessageIdChanged = false;
                        }
                        catch
                        {
                        }
                    }
                }
            }
            finally
            {
                LastMessageIdUpdateThread = null;
            }
        }
        private static void KeepAliveThread()
        {
            SignalRHostStateSet set = new SignalRHostStateSet();

            try
            {
                evt.Set();
                int failCount = 0;
                var cntx      = Cntx;
                var hsvc      = new SignalRHostStateServiceProxy();
                var host      = hsvc.LoadEntityByNature(cntx, config.HostName, config.App.ID).SingleOrDefault();
                if (host == null)
                {
                    host = new SignalRHostState
                    {
                        HostName      = config.HostName,
                        ApplicationID = config.App.ID,
                        LastMsgId     = 0
                    };
                    var x = hsvc.AddOrUpdateEntities(cntx, new SignalRHostStateSet(), new SignalRHostState[] { host });
                    host = x.ChangedEntities[0].UpdatedItem;
                }
                while (true)
                {
                    Thread.Sleep(1000 * config.HostStateUpdateIntervalInSeconds);
                    hsvc = new SignalRHostStateServiceProxy();
                    if (IsLastMessageIdChanged)
                    {
                        host = new SignalRHostState
                        {
                            IsPersisted         = true,
                            HostName            = config.HostName,
                            ApplicationID       = config.App.ID,
                            LastMsgId           = (long)LastMessageId,
                            IsLastMsgIdModified = true
                        };
                        try
                        {
                            hsvc.AddOrUpdateEntities(cntx, set, new SignalRHostState[] { host });
                            IsLastMessageIdChanged = false;
                            if (failCount > 0)
                            {
                                Current.Subscribe();
                                failCount = 0;
                            }
                        }
                        catch
                        {
                            failCount++;
                        }
                    }
                    else
                    {
                        try
                        {
                            bool b = Current.PollMessages(cntx, LastMessageId);
                            if (b || (Current.lastSent - Current.lastReceived) > TimeSpan.FromSeconds(20) || failCount > 0)
                            {
                                Current.Subscribe();
                                failCount = 0;
                            }
                        }
                        catch
                        {
                            failCount++;
                        }
                    }
                    if (Current.CallbackFailed)
                    {
                        try
                        {
                            Current.Subscribe();
                            failCount = 0;
                        }
                        catch
                        {
                            failCount++;
                        }
                    }
                }
            }
            finally
            {
                LastMessageIdUpdateThread = null;
            }
        }