private bool ProcessServerMessage(string line, int spacePos)
        {
            // Get the number out of the message
            // :wormnet1.team17.com 322 Test #Help 10 :05 A place to get help, or help others
            int number;
            int spacePos2 = line.IndexOf(' ', spacePos + 1);

            if (spacePos2 == -1 || !int.TryParse(line.Substring(spacePos + 1, spacePos2 - spacePos - 1), out number))
            {
                return(false);
            }

            // Find the space after our nickname in the message
            spacePos = line.IndexOf(' ', spacePos2 + 1);
            if (spacePos == -1)
            {
                return(false);
            }
            spacePos++; // we will start all the regex match from this position

            // Process the message
            switch (number)
            {
            // Connection success
            case 4:
                spacePos2 = line.IndexOf(' ', spacePos);
                if (spacePos2 != -1)
                {
                    this.serverIrcAddress = ':' + line.Substring(spacePos, spacePos2 - spacePos).ToLower();

                    //if (!this.IsWormNet)
                    //    Send("authserv auth " + this.User.Name + " " + Properties.Settings.Default.WormsPassword);

                    if (ConnectionState != null)
                    {
                        ConnectionState.BeginInvoke(this, new ConnectionStateEventArgs(ConnectionStates.Connected), null, null);
                    }
                }
                break;

            // This nickname is already in use!
            case 433:
                if (serverIrcAddress == string.Empty)
                {
                    return(true);
                }
                else
                {
                    // nickname is in use when we tried to change with /NICK command
                    GlobalManager.UITasks.Enqueue(new NickNameInUseTask(this));
                }
                break;

            // A channel (answer for the LIST command)
            case 322:
                // :wormnet1.team17.com 322 Test #Help 10 :05 A place to get help, or help others
                Match chMatch = channelRegex.Match(line, spacePos);
                if (chMatch.Success)
                {
                    string channelName = chMatch.Groups[1].Value;
                    string description = chMatch.Groups[3].Value;

                    if (!channelListHelper.ContainsKey(channelName))
                    {
                        channelListHelper.Add(channelName, description);
                    }
                }
                break;

            // LIST END
            case 323:
                GlobalManager.UITasks.Enqueue(new ChannelListUITask(this, channelListHelper));
                break;

            // A client (answer for WHO command)
            case 352:
                // :wormnet1.team17.com 352 Test #AnythingGoes ~UserName no.address.for.you wormnet1.team17.com Herbsman H :0 68 7 LT The Wheat Snooper 2.8
                Match clMatch = clientRegex.Match(line, spacePos);
                if (clMatch.Success)
                {
                    string   channelHash = clMatch.Groups[1].Value.ToLower();
                    string   clan        = clMatch.Groups[2].Value.ToLower() == "username" ? string.Empty : clMatch.Groups[2].Value;
                    string   clientName  = clMatch.Groups[3].Value;
                    string[] realName    = clMatch.Groups[4].Value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);  // 68 7 LT The Wheat Snooper

                    CountryClass country            = CountriesClass.DefaultCountry;
                    int          rank               = 0;
                    bool         clientGreatSnooper = false;
                    string       clientApp          = clMatch.Groups[4].Value;

                    if (realName.Length >= 3)
                    {
                        if (int.TryParse(realName[1], out rank))
                        {
                            if (rank > 13)
                            {
                                rank = 13;
                            }
                            if (rank < 0)
                            {
                                rank = 0;
                            }
                        }

                        int countrycode;
                        if (int.TryParse(realName[0], out countrycode) && countrycode >= 0 && countrycode <= 52)
                        {
                            if (countrycode == 49 && realName[2].Length == 2)     // use cc as countricode
                            {
                                if (realName[2] == "UK")
                                {
                                    realName[2] = "GB";
                                }
                                else if (realName[2] == "EL")
                                {
                                    realName[2] = "GR";
                                }
                                country = CountriesClass.GetCountryByCC(realName[2]);
                            }
                            else
                            {
                                country = CountriesClass.GetCountryByID(countrycode);
                            }
                        }
                        else if (realName[2].Length == 2)     // use cc if countrycode is bigger than 52
                        {
                            if (realName[2] == "UK")
                            {
                                realName[2] = "GB";
                            }
                            else if (realName[2] == "EL")
                            {
                                realName[2] = "GR";
                            }
                            country = CountriesClass.GetCountryByCC(realName[2]);
                        }

                        clientGreatSnooper = realName.Length >= 6 && realName[3] == "Great" && realName[4] == "Snooper" && gsVersionRegex.IsMatch(realName[5]);
                        StringBuilder sb = new StringBuilder();
                        for (int i = 3; i < realName.Length; i++)
                        {
                            sb.Append(realName[i]);
                            if (i + 1 < realName.Length)
                            {
                                sb.Append(" ");
                            }
                        }
                        clientApp = sb.ToString();
                    }

                    GlobalManager.UITasks.Enqueue(new ClientUITask(this, channelHash, clientName, country, clan, rank, clientGreatSnooper, clientApp));
                }
                break;

            // The user is offline message
            case 401:
                // :wormnet1.team17.com 401 Test sToOMiToO :No such nick/channel
                spacePos2 = line.IndexOf(' ', spacePos);
                if (spacePos2 != -1)
                {
                    string clientName = line.Substring(spacePos, spacePos2 - spacePos);
                    GlobalManager.UITasks.Enqueue(new OfflineUITask(this, clientName));
                }
                break;
            }

            return(false);
        }
        public Login()
        {
            if (firstStart)
            {
                WormNetCharTable.Initialize();

                if (!Properties.Settings.Default.SettingsUpgraded)
                {
                    try
                    {
                        Properties.Settings.Default.Upgrade();
                        if (Properties.Settings.Default.Group0List.Length == 0)
                        {
                            Properties.Settings.Default.Group0List = Properties.Settings.Default.BuddyList;
                        }

                        // new colors
                        if (Properties.Settings.Default.ChannelMessageStyle == "F0FFFF|13|0|0|0|0|Tahoma")
                        {
                            Properties.Settings.Default.ChannelMessageStyle = (string)Properties.Settings.Default.Properties["ChannelMessageStyle"].DefaultValue;
                        }
                        if (Properties.Settings.Default.JoinMessageStyle == "808000|12|0|0|0|0|Tahoma")
                        {
                            Properties.Settings.Default.JoinMessageStyle = (string)Properties.Settings.Default.Properties["JoinMessageStyle"].DefaultValue;
                        }
                        if (Properties.Settings.Default.PartMessageStyle == "808000|12|0|0|0|0|Tahoma")
                        {
                            Properties.Settings.Default.PartMessageStyle = (string)Properties.Settings.Default.Properties["PartMessageStyle"].DefaultValue;
                        }
                        if (Properties.Settings.Default.QuitMessageStyle == "808000|12|0|0|0|0|Tahoma")
                        {
                            Properties.Settings.Default.QuitMessageStyle = (string)Properties.Settings.Default.Properties["QuitMessageStyle"].DefaultValue;
                        }
                        if (Properties.Settings.Default.OfflineMessageStyle == "FF0000|13|0|0|0|0|Tahoma")
                        {
                            Properties.Settings.Default.OfflineMessageStyle = (string)Properties.Settings.Default.Properties["OfflineMessageStyle"].DefaultValue;
                        }
                        if (Properties.Settings.Default.ActionMessageStyle == "FFFF00|13|0|0|0|0|Tahoma")
                        {
                            Properties.Settings.Default.ActionMessageStyle = (string)Properties.Settings.Default.Properties["ActionMessageStyle"].DefaultValue;
                        }
                        if (Properties.Settings.Default.UserMessageStyle == "E9967A|13|0|0|0|0|Tahoma")
                        {
                            Properties.Settings.Default.UserMessageStyle = (string)Properties.Settings.Default.Properties["UserMessageStyle"].DefaultValue;
                        }
                        if (Properties.Settings.Default.NoticeMessageStyle == "E9967A|13|0|0|0|0|Tahoma")
                        {
                            Properties.Settings.Default.NoticeMessageStyle = (string)Properties.Settings.Default.Properties["NoticeMessageStyle"].DefaultValue;
                        }
                    }
                    catch (Exception) { }

                    var    validator   = new GSVersionValidator();
                    string quitMessage = Properties.Settings.Default.QuitMessagee;
                    if (Properties.Settings.Default.QuitMessagee == string.Empty || validator.Validate(ref quitMessage) != string.Empty)
                    {
                        Properties.Settings.Default.QuitMessagee = "Great Snooper v" + App.GetVersion();
                    }
                    Properties.Settings.Default.SettingsUpgraded = true;
                    Properties.Settings.Default.Save();
                }

                MessageSettings.Initialize();
                CountriesClass.Initialize();
                GlobalManager.Initialize();
                UserGroups.Initialize();
                Sounds.Initialize();

                // Reducing Timeline frame rate
                Timeline.DesiredFrameRateProperty.OverrideMetadata(
                    typeof(Timeline),
                    new FrameworkPropertyMetadata {
                    DefaultValue = 25
                }
                    );
            }
            InitializeComponent();
            DataContext = this;

            nickRegex  = new Regex(@"^[a-z`]", RegexOptions.IgnoreCase);
            nickRegex2 = new Regex(@"^[a-z`][a-z0-9`\-]*$", RegexOptions.IgnoreCase);

            ServerList = new SortedObservableCollection <string>();
            ServerList.DeSerialize(Properties.Settings.Default.ServerAddresses);

            Server.SelectedItem = Properties.Settings.Default.ServerAddress;

            switch (Properties.Settings.Default.LoginType)
            {
            case "simple":
                LoginTypeChooser.SelectedIndex = 0;
                break;

            default:
                LoginTypeChooser.SelectedIndex = 1;
                break;
            }

            AutoLogIn.IsChecked = Properties.Settings.Default.AutoLogIn;
            Nick.Text           = Properties.Settings.Default.UserName;

            Country.ItemsSource = CountriesClass.Countries;
            if (Properties.Settings.Default.UserCountry != -1)
            {
                CountryClass country = CountriesClass.GetCountryByID(Properties.Settings.Default.UserCountry);
                Country.SelectedItem = country;
            }
            else
            {
                CultureInfo ci = CultureInfo.InstalledUICulture;

                CountryClass country;
                if (ci != null)
                {
                    country = CountriesClass.GetCountryByCC(ci.TwoLetterISOLanguageName.ToUpper());
                }
                else
                {
                    country = CountriesClass.DefaultCountry;
                }

                Country.SelectedItem = country;
            }

            Rank.ItemsSource   = RanksClass.Ranks;
            Rank.SelectedIndex = Properties.Settings.Default.UserRank;
            Clan.Text          = Properties.Settings.Default.UserClan;

            TUSNick.Text     = Properties.Settings.Default.TusNick;
            TUSPass.Password = Properties.Settings.Default.TusPass;
        }