public bool ErrorLog(string ConnectionId, string ErrorMsg)
        {
            bool resultLog = false;

            try
            {
                if (!string.IsNullOrEmpty(ConnectionId) && !string.IsNullOrEmpty(ErrorMsg))
                {
                    using (var db = new LicensingEntities())
                    {
                        Guid convert = Guid.Parse(ConnectionId);

                        var connectionItems = db.Connection.Where(id => id.ConnectionID == convert);

                        if (connectionItems.Count() > 0)
                        {
                            var connectionItem = connectionItems.First();

                            connectionItem.ErrorLog = ErrorMsg;

                            db.SaveChanges();

                            resultLog = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                resultLog = false;
                System.Diagnostics.Debug.WriteLine(ex);
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            return resultLog;
        }
        public bool IsConnectionStop(string ConnectionId)
        {
            bool result = false;

            try
            {
                if (!string.IsNullOrEmpty(ConnectionId))
                {
                    Guid connection = Guid.Parse(ConnectionId);

                    using (var db = new LicensingEntities())
                    {
                        var Connections = db.Connection.Where(con => con.ConnectionID == connection);

                        if (Connections.Count() > 0)
                        {
                            var connectionItem = Connections.First();

                            if (connectionItem.StopCalled.HasValue)
                            {
                                if (!connectionItem.StopCalled.Value && !connectionItem.ReStarted)
                                {
                                    result = true;
                                    connectionItem.ReStarted = true;
                                    db.SaveChanges();
                                }
                                else
                                {
                                    result = false;
                                }

                            }
                            else
                            {
                                result = false;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result = false;
                System.Diagnostics.Debug.WriteLine(ex);
            }finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            return result;

        }
        public void OnDisConnection(string ConnectionId, string UserName, DateTime currentDateTime, bool stopCall)
        {
            logWrite( ConnectionId,"DisConnection Start");

            var connectionId = Guid.Parse(ConnectionId);

             using (var db = new LicensingEntities())
            {
                var users = db.User.Where(f => f.UserName == UserName);

                if (users.Count() > 0)
                {
                    var user = users.First();
                    user.LastActivity = currentDateTime;
                }

                var _connections = db.Connection.Where(con => con.ConnectionID == connectionId);

                if (_connections.Count() > 0)
                {
                    var _connection = _connections.First();
                    _connection.Status = (int)ConnectionStatus.OnDisConnected;
                    _connection.LastActivity = currentDateTime;
                    _connection.StopCalled = stopCall;
                    _connection.DisConnectedDateTime = currentDateTime;
                }

                db.SaveChanges();
            }

            Clients.Group(((int)Role.manager).ToString()).DisConnection(ConnectionId);

            logWrite(ConnectionId, "DisConnection End");
        }
        private void SetReConnection(ConnectionInfo reConnectionInfo)
        {
            Guid ConnectionID = Guid.Parse(reConnectionInfo.ConnectionId);

            using (var db = new LicensingEntities())
            {
                var users = db.User.Where(f => f.UserName == reConnectionInfo.UserName);

                if (users.Count() > 0)
                {
                    var user = users.First();
                    user.LastActivity = reConnectionInfo.CurrentDateTime;
                }
                else
                {
                    var user = new User
                    {
                        ID = Guid.NewGuid(),
                        UserName = reConnectionInfo.UserName,
                        Company = reConnectionInfo.Company,
                        ProjectType = reConnectionInfo.ProjectType,
                        Role = 1,
                        LastActivity = reConnectionInfo.CurrentDateTime
                    };

                    db.User.Add(user);
                }

                var _connections = db.Connection.Where(con => con.ConnectionID == ConnectionID);

                if (_connections.Count() > 0)
                {
                    var _connection = _connections.First();
                    _connection.ReConnectedDateTime = reConnectionInfo.CurrentDateTime;
                    _connection.LastActivity = reConnectionInfo.CurrentDateTime;
                    _connection.Status = (int)ConnectionStatus.OnReConnected;
                }
                else
                {
                    CreateConnection(reConnectionInfo);
                }

                db.SaveChanges();

            }
        }
        private void CreateConnection(ConnectionInfo connectionInfo)
        {
            logWrite(connectionInfo.ConnectionId, "Connection 생성 중입니다.");

            using (var db = new LicensingEntities())
            {
                var _connection = new Connection
                {
                    ConnectionID = Guid.Parse(connectionInfo.ConnectionId),
                    UserAgent = (connectionInfo.UserAgent != null ? connectionInfo.UserAgent : string.Empty),
                    Company = connectionInfo.Company,
                    ProjectType = connectionInfo.ProjectType,
                    ConnectedDateTime = connectionInfo.CurrentDateTime,
                    Transport = connectionInfo.Transport,
                    LastActivity = connectionInfo.CurrentDateTime,
                    UserName = connectionInfo.UserName,
                    ExtensionV1 = connectionInfo.ExtensionV1,
                    ExtensionV2 = connectionInfo.ExtensionV2,
                    Status = connectionInfo.Status
                };

                db.Connection.Add(_connection);
                db.SaveChanges();

            }
        } 
        private LicenseUser CreateLicense(ConnectionInfo connectionInfo, int LicenseType)
        {
            LicenseUser newLicense = new LicenseUser();

            logWrite(connectionInfo.ConnectionId,"라이센스 타입 : " + LicenseType + "라이센스 생성 중..");

            try
            {
                using (var db = new LicensingEntities())
                {
                    newLicense = new LicenseUser()
                    {
                        ID = Guid.NewGuid(),
                        Company = connectionInfo.Company,
                        ProjectType = connectionInfo.ProjectType,
                        UserName = connectionInfo.UserName,
                        LicenseType = LicenseType,
                        StartDateTime = connectionInfo.CurrentDateTime,
                        EndDateTime = connectionInfo.CurrentDateTime.AddMinutes(LicenseType)
                    };

                    db.LicenseUser.Add(newLicense);
                    db.SaveChanges();
                   
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
                newLicense = null;
            }

            return newLicense;
        }
        private void SetConnection(ConnectionInfo connectionInfo, LicenseUser licenseUser)
        {
            using (var db = new LicensingEntities())
            {
                Guid ConnectionID = Guid.Parse(connectionInfo.ConnectionId);

                var users = db.User.Where(f => f.UserName == connectionInfo.UserName);

                if (users.Count() == 0)
                {
                    int Isrole;

                    if(!int.TryParse(connectionInfo.Role, out Isrole))
                    {
                        Isrole = 1;
                    }

                    var user = new User
                    {
                        ID = Guid.NewGuid(),
                        UserName = connectionInfo.UserName,
                        Company = connectionInfo.Company,
                        ProjectType = connectionInfo.ProjectType,
                        Role = Isrole,
                        LastActivity = connectionInfo.CurrentDateTime
                    };

                    db.User.Add(user);
                }
                else
                {
                    var user = users.First();
                    user.LastActivity = DateTime.UtcNow;
                }

                db.SaveChanges();

                var _connections = db.Connection.Where(con => con.ConnectionID == ConnectionID);

                if (_connections.Count() == 0)
                {
                    CreateConnection(connectionInfo);
                }

                Clients.Client(connectionInfo.ConnectionId).licenseExpired(licenseUser.EndDateTime);
            }
        }