private async Task garenaLogin()
        {
            WindowsIdentity winIdentity = WindowsIdentity.GetCurrent();
            WindowsPrincipal winPrincipal = new WindowsPrincipal(winIdentity);
            if (!winPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                if (await ((MetroWindow)Application.Current.MainWindow).ShowMessageAsync("Insufficent Privledges.", "Press OK to restart as admin.") == MessageDialogResult.Affirmative)
                {
                    var info = new ProcessStartInfo(Path.Combine(Client.ExecutingDirectory, "Client", "LegendaryClient.exe"))
                    {
                        UseShellExecute = true,
                        Verb = "runas"
                    };
                    Process.Start(info);
                    Environment.Exit(0);
                }
            }

            LoggingInLabel.Content = "Waiting for user to launch League from garena";
            HideGrid.Visibility = Visibility.Hidden;
            ErrorTextBox.Visibility = Visibility.Hidden;
            LoggingInLabel.Visibility = Visibility.Visible;
            var garenaregion = BaseRegion.GetRegion((string)RegionComboBox.SelectedValue);
            LoggingInProgressRing.Visibility = Visibility.Visible;
            LoginPasswordBox.Visibility = Visibility.Hidden;
            shouldExit = false;
            while (!shouldExit)
            {
                await Task.Delay(500);
                foreach (var process in Process.GetProcessesByName("lol"))
                {
                    var s1 = GetCommandLine(process);
                    process.Kill();
                    foreach (var lolclient in Process.GetProcessesByName("LolClient"))
                    {
                        lolclient.Kill();
                    }
                    s1 = s1.Substring(1);
                    Client.Log("Received token, it is: " + s1);
                    Client.Region = garenaregion;
                    Dispatcher.BeginInvoke(
                        DispatcherPriority.Input, new ThreadStart(() =>
                            {
                                HideGrid.Visibility = Visibility.Hidden;
                                ErrorTextBox.Visibility = Visibility.Hidden;
                                LoggingInLabel.Visibility = Visibility.Visible;
                                LoggingInLabel.Content = "Logging in...";
                                LoggingInProgressRing.Visibility = Visibility.Visible;
                            }));
                    var context = RiotCalls.RegisterObjects();
                    Client.RiotConnection = new RtmpClient(new System.Uri("rtmps://" + garenaregion.Server + ":2099"), context, ObjectEncoding.Amf3);
                    Client.RiotConnection.CallbackException += client_CallbackException;
                    Client.RiotConnection.MessageReceived += client_MessageReceived;
                    await Client.RiotConnection.ConnectAsync();
                    Client.RiotConnection.SetChunkSize(2147483647);

                    AuthenticationCredentials newCredentials = new AuthenticationCredentials
                    {
                        AuthToken = await
                            RiotCalls.GetRestToken(LoginUsernameBox.Text, LoginPasswordBox.Password, garenaregion.LoginQueue, reToken(s1)),
                        Username = Client.UID,
                        Password = null,
                        ClientVersion = Client.Version,
                        IpAddress = RiotCalls.GetIpAddress(),
                        Locale = garenaregion.Locale,
                        PartnerCredentials = "8393 " + s1,
                        OperatingSystem = "Windows 7",
                        Domain = "lolclient.lol.riotgames.com",
                    };
                    Session login = await RiotCalls.Login(newCredentials);
                    Client.PlayerSession = login;
                    var str1 = string.Format("gn-{0}", login.AccountSummary.AccountId);
                    var str2 = string.Format("cn-{0}", login.AccountSummary.AccountId);
                    var str3 = string.Format("bc-{0}", login.AccountSummary.AccountId);
                    Task<bool>[] taskArray = { Client.RiotConnection.SubscribeAsync("my-rtmps", "messagingDestination", str1, str1), 
                                                 Client.RiotConnection.SubscribeAsync("my-rtmps", "messagingDestination", str2, str2), 
                                                 Client.RiotConnection.SubscribeAsync("my-rtmps", "messagingDestination", "bc", str3) };

                    await Task.WhenAll(taskArray);
                    var LoggedIn = await Client.RiotConnection.LoginAsync(Client.UID, login.Token);
                    //var packet = await RiotCalls.GetLoginDataPacketForUser();
                    DoGetOnLoginPacket();
                }
            }
        }
        private async void LoginButton_Click(object sender, RoutedEventArgs args)
        {
            if (RememberPasswordCheckbox.IsChecked == true)
                Properties.Settings.Default.SavedPassword = LoginPasswordBox.Password;
            else
                Properties.Settings.Default.SavedPassword = "";

            if (RememberUsernameCheckbox.IsChecked == true)
                Properties.Settings.Default.SavedUsername = LoginUsernameBox.Text;
            else
                Properties.Settings.Default.SavedUsername = "";

            Properties.Settings.Default.AutoLogin = (bool)AutoLoginCheckBox.IsChecked;
            Properties.Settings.Default.Region = (string)RegionComboBox.SelectedValue;
            Properties.Settings.Default.Save();

            HideGrid.Visibility = Visibility.Hidden;
            ErrorTextBox.Visibility = Visibility.Hidden;
            LoggingInLabel.Visibility = Visibility.Visible;
            LoggingInProgressRing.Visibility = Visibility.Visible;
            BaseRegion SelectedRegion = BaseRegion.GetRegion((string)RegionComboBox.SelectedValue);
            Client.Region = SelectedRegion;

            var context = RiotCalls.RegisterObjects();
            Client.RtmpConnection = new RtmpClient(new Uri("rtmps://" + SelectedRegion.Server + ":2099"), context, ObjectEncoding.Amf3);
            Client.RtmpConnection.MessageReceived += Client.OnMessageReceived;
            Client.RtmpConnection.CallbackException += Client.CallbackException;
            RiotCalls.OnInvocationError += Client.CallbackException;
            await Client.RtmpConnection.ConnectAsync();

            AuthenticationCredentials newCredentials = new AuthenticationCredentials();
            newCredentials.Username = LoginUsernameBox.Text;
            newCredentials.Password = LoginPasswordBox.Password;
            newCredentials.ClientVersion = Client.Version;
            newCredentials.IpAddress = RiotCalls.GetIpAddress();
            newCredentials.Locale = SelectedRegion.Locale;
            newCredentials.Domain = "lolclient.lol.riotgames.com";
            try
            {
                newCredentials.AuthToken = RiotCalls.GetAuthKey(LoginUsernameBox.Text, LoginPasswordBox.Password, SelectedRegion.LoginQueue);
            }
            catch (Exception e)
            {
                HideGrid.Visibility = Visibility.Visible;
                ErrorTextBox.Visibility = Visibility.Visible;
                LoggingInProgressRing.Visibility = Visibility.Hidden;
                LoggingInLabel.Visibility = Visibility.Hidden;
                if (e.Message.Contains("The remote name could not be resolved"))
                    ErrorTextBox.Text = "Please make sure you are connected the internet!";
                else if (e.Message.Contains("(403) Forbidden"))
                    ErrorTextBox.Text = "Your username or password is incorrect!";
                else
                    ErrorTextBox.Text = "Unable to get Auth Key";

                ErrorTextBox.Text += string.Format("{0}{1}{0}{2}", Environment.NewLine, e.Message, e.StackTrace);
                return;
            }

            Session login = await RiotCalls.Login(newCredentials);
            Client.PlayerSession = login;
            await Client.RtmpConnection.SubscribeAsync("my-rtmps", "messagingDestination", "bc", "bc-" + login.AccountSummary.AccountId.ToString());
            await Client.RtmpConnection.SubscribeAsync("my-rtmps", "messagingDestination", "gn-" + login.AccountSummary.AccountId.ToString(), "gn-" + login.AccountSummary.AccountId.ToString());
            await Client.RtmpConnection.SubscribeAsync("my-rtmps", "messagingDestination", "cn-" + login.AccountSummary.AccountId.ToString(), "cn-" + login.AccountSummary.AccountId.ToString());
            bool LoggedIn = await Client.RtmpConnection.LoginAsync(LoginUsernameBox.Text.ToLower(), login.Token);
            LoginDataPacket packet = await RiotCalls.GetLoginDataPacketForUser();
            string State = await RiotCalls.GetAccountState();

            if (State != "ENABLED")
            {
                HideGrid.Visibility = Visibility.Visible;
                ErrorTextBox.Visibility = Visibility.Visible;
                LoggingInProgressRing.Visibility = Visibility.Hidden;
                LoggingInLabel.Visibility = Visibility.Hidden;
                ErrorTextBox.Text = "Your account state was invalid: " + State;
                return;
            }

            GotLoginPacket(packet);
        }
#pragma warning disable 4014 //Code does not need to be awaited
        async void Login()
        {
            BaseRegion selectedRegion = BaseRegion.GetRegion((string)RegionComboBox.SelectedValue);
            var authToken = await RiotCalls.GetRestToken(LoginUsernameBox.Text, LoginPasswordBox.Password, selectedRegion.LoginQueue);

            if (authToken == "invalid_credentials")
            {
                ErrorTextBox.Text = "Wrong login data";
                HideGrid.Visibility = Visibility.Visible;
                ErrorTextBox.Visibility = Visibility.Visible;
                LoggingInLabel.Visibility = Visibility.Hidden;
                LoggingInProgressRing.Visibility = Visibility.Collapsed;
                return;
            }
            Client.RiotConnection = new RtmpClient(new System.Uri("rtmps://" + selectedRegion.Server + ":2099"), RiotCalls.RegisterObjects(), ObjectEncoding.Amf3);
            Client.RiotConnection.CallbackException += client_CallbackException;
            Client.RiotConnection.MessageReceived += client_MessageReceived;
            try
            {

                await Client.RiotConnection.ConnectAsync();
            }
            catch
            {
                ErrorTextBox.Text = "Failed to login. SSL error.";
                HideGrid.Visibility = Visibility.Visible;
                ErrorTextBox.Visibility = Visibility.Visible;
                LoggingInLabel.Visibility = Visibility.Hidden;
                LoggingInProgressRing.Visibility = Visibility.Collapsed;
                return;
            }

            Client.RiotConnection.SetChunkSize(2147483647);
            AuthenticationCredentials newCredentials = new AuthenticationCredentials
            {
                Username = LoginUsernameBox.Text,
                Password = LoginPasswordBox.Password,
                ClientVersion = Client.Version,
                IpAddress = RiotCalls.GetIpAddress(),
                Locale = selectedRegion.Locale,
                OperatingSystem = "Windows 7",
                Domain = "lolclient.lol.riotgames.com",
                AuthToken = authToken
            };
            Session login = await RiotCalls.Login(newCredentials);
            if (login == null)
            {
                Client.Log("Login session is null.");
                var overlay = new MessageOverlay
                {
                    MessageTextBox = { Text = "Login session is null. Login failed. Please check whether the version number is correct or not.", IsReadOnly = true },
                    MessageTitle = { Content = "Login session is null." }
                };
                Client.OverlayContainer.Content = overlay.Content;
                Client.OverlayContainer.Visibility = Visibility.Visible;
                ErrorTextBox.Text = "Failed to login. Login session is null.";
                HideGrid.Visibility = Visibility.Visible;
                ErrorTextBox.Visibility = Visibility.Visible;
                LoggingInLabel.Visibility = Visibility.Hidden;
                LoggingInProgressRing.Visibility = Visibility.Collapsed;
                return;
            }
            Client.PlayerSession = login;
            var str1 = string.Format("gn-{0}", login.AccountSummary.AccountId);
            var str2 = string.Format("cn-{0}", login.AccountSummary.AccountId);
            var str3 = string.Format("bc-{0}", login.AccountSummary.AccountId);
            Task<bool>[] taskArray = { Client.RiotConnection.SubscribeAsync("my-rtmps", "messagingDestination", str1, str1), 
                                                 Client.RiotConnection.SubscribeAsync("my-rtmps", "messagingDestination", str2, str2), 
                                                 Client.RiotConnection.SubscribeAsync("my-rtmps", "messagingDestination", "bc", str3) };

            await Task.WhenAll(taskArray);
            //Riot added this for no reason but make it look like the riot client we have to do this
            var plainTextbytes = Encoding.UTF8.GetBytes(login.AccountSummary.Username + ":" + login.Token);
            Client.reconnectToken = Convert.ToBase64String(plainTextbytes);
            //await RiotCalls.Login(result);
            var LoggedIn = await Client.RiotConnection.LoginAsync(LoginUsernameBox.Text.ToLower(), login.Token);
            DoGetOnLoginPacket();
        }