private static string GetCurrentSession()
        {
            var cookie = HttpContext.Current.Request.Cookies.Get(CookieActions.GetSessionCookieName());

            if (cookie != null)
            {
                return(cookie.Value);
            }
            return("");
        }
        protected void Application_AcquireRequestState(Object sender, EventArgs e)
        {
            Context.Items["osCurrentPTAName"]     = RequestPtaName;
            Context.Items["osCurrentPTAUserName"] = RequestPtaUserName;
            Context.Items["osIsLoadingScreen"]    = false;
            RunningInfo.InitializeRunningInfo();
            RunningInfo.ESpaceHash         = ConfigurationManager.AppSettings["OutSystems.HubEdition.EspaceCompilationHash"];
            RunningInfo.ESpaceVersionToken = ConfigurationManager.AppSettings["OutSystems.HubEdition.EspaceVersionToken"];
            RunningInfo.ESpaceVersionId    = int.Parse(ConfigurationManager.AppSettings["OutSystems.HubEdition.EspaceVersionID"]);
            RunningInfo.DebugMode          = false;
            // Skips internal pages
            if (Request.FilePath.ToLowerInvariant().EndsWith("/_ping.aspx") || Request.FilePath.ToLowerInvariant().EndsWith("/_queriescoverage.aspx") || Request.FilePath.ToLower().EndsWith("/_debugger.asmx") || Request.FilePath.ToLower().EndsWith("/_debuggerevents.ashx"))
            {
                return;
            }

            if (App == null)
            {
                // Try again
                Application_Start(sender, e);
                if (Application["ApplicationStartError"] != null)
                {
                    ApplicationStartErrorRedirect();
                }
            }

            if (App.Tenant.PhoneConfigException != null)
            {
                Application["ApplicationStartError"] = App.Tenant.PhoneConfigException;
                ApplicationStartErrorRedirect();
            }

            // Session Start
            HeContext heContext = Global.App.OsContext;

            heContext.InitSession();

            var sessionCookieKey = Response.Cookies.AllKeys.FirstIfSingleOrDefault(c => c == CookieActions.GetSessionCookieName());

            if (sessionCookieKey != null)
            {
                var sessionCookie = Response.Cookies.Get(sessionCookieKey);
                if (sessionCookie != null && Settings.GetBool(Settings.Configs.EnforceSessionCookiesSecure))
                {
                    sessionCookie.Secure = true;
                }
            }

            if (Context.Session == null || heContext.Session.NeedsSessionStart(App.eSpaceName))
            {
                if (Request.Path.ToLowerInvariant().EndsWith("_SmsHandler.aspx"))
                {
                    Global.App.OsContext.Session["TerminalType"] = "SMS";
                    Global.App.OsContext.Session["MSISDN"]       = Global.App.OsContext.MOMsg.MSISDN;
                }
                else if (Request.Path.ToLowerInvariant().EndsWith("smshandler.asmx"))
                {
                    Global.App.OsContext.Session["TerminalType"] = "SMS";
                    Global.App.OsContext.Session["MSISDN"]       = Request.Headers["MSISDN"];
                }
                else
                {
                    Global.App.OsContext.Session["TerminalType"] = "WEB";
                    Global.App.OsContext.Session["MSISDN"]       = "";
                }

                if (Application["ApplicationStartError"] != null)
                {
                    // Try again
                    Application_Start(sender, e);
                }

                if (Application["ApplicationStartError"] != null)
                {
                    ApplicationStartErrorRedirect();
                }
                if (Context.Session != null)
                {
                    ExtendedActions.AutoLogin(App, App.OsContext.Session);
                    RunOnSessionStart();
                }
            }            /*
                          * else {
                          *
                          * } */

            // Process visit cookies
            if (RuntimePlatformUtils.ShouldCreateCookieForRequest())
            {
                if (Request.CurrentExecutionFilePath.ToLowerInvariant().EndsWith(".aspx"))
                {
                    var osVisitorCookie = Request.Cookies["osVisitor"];
                    var osVisitCookie   = Request.Cookies["osVisit"];

                    if (osVisitorCookie == null || !GuidUtils.IsGuid(osVisitorCookie.Value))
                    {
                        osVisitorCookie         = new HttpCookie("osVisitor", Guid.NewGuid().ToString());
                        osVisitorCookie.Expires = DateTime.Now.AddYears(100);                         // forever
                        SecureCookieUtils.setSecureCookie(osVisitorCookie, heContext.Context.Response);
                    }

                    if (osVisitCookie == null || !GuidUtils.IsGuid(osVisitCookie.Value))
                    {
                        osVisitCookie = new HttpCookie("osVisit", Guid.NewGuid().ToString());
                        heContext.Session["osIsNewVisit"] = true;
                    }
                    osVisitCookie.Expires = DateTime.Now.AddMinutes(30);
                    SecureCookieUtils.setSecureCookie(osVisitCookie, heContext.Context.Response);

                    Context.Items["osVisitor"] = osVisitorCookie.Value;
                    Context.Items["osVisit"]   = osVisitCookie.Value;
                }
            }

            App.OsContext.Session[GenericExtendedActions.ReqAuditCountSessionName] = 0;

            // Default Multilingual state
            if (!App.MultilingualEnabled)
            {
                GenericExtendedActions.SetCurrentLocale(heContext, "");
            }
            else
            {
                string localeHeader = heContext.OsISAPIFilter.GetLocale(Request);
                if (localeHeader != null)
                {
                    try {
                        GenericExtendedActions.SetCurrentLocale(heContext, localeHeader);
                    } catch {}
                }
            }
        }