private void RemoteClient_PacketReceived(Client sender, Client.PacketReceivedEventArgs e)
        {
            PacketType packetType = (PacketType)e.Reader.ReadInt16();

            //Disconnect the user if he attempts to do anything else before authenticating.
            if (packetType != PacketType.Authenticate && !Authenticated)
            {
                Disconnect("Your attempt at sending packets before authenticating has been ignored!");
                return;
            }
            switch (packetType)
            {
                case PacketType.Authenticate:
                    InterfaceType = (InterfaceType)e.Reader.ReadByte();
                    int major = e.Reader.ReadInt32();
                    int minor = e.Reader.ReadInt32();
                    if (Rtc.buildVersion.Major != major || Rtc.buildVersion.Minor != minor)
                    {
                        Disconnect($"Your version ({major}.{minor}) is incompatible with the server's version ({Rtc.buildVersion.Major}.{Rtc.buildVersion.Minor}).");
                        return;
                    }
                    string Username = e.Reader.ReadString();
                    string Password = e.Reader.ReadString();
                    TSUser = TShock.Users.GetUserByName(Username);

                    if (TSUser == null || !TSUser.VerifyPassword(Password))
                    {
                        Disconnect("Invalid username/password or insufficient privileges.");
                        return;
                    }
                    Group g = TShock.Groups.GetGroupByName(TSUser.Group);

                    if (!g.HasPermission("*"))
                    {
                        Disconnect("Invalid username/password or insufficient privileges.");
                        return;
                    }
                    Authenticated = true;
                    Packet pck = new Packet((short)PacketType.MessageBuffer, (short)Rtc.MessagesBuffer.Length);
                    for (int i = 0; i < Rtc.MessagesBuffer.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(Rtc.MessagesBuffer[i]))
                           pck.Write(Rtc.ColorBuffer[i], Rtc.MessagesBuffer[i]);

                    }
                    sender.Send(pck);
                    break;
                case PacketType.Input:
                    string text = e.Reader.ReadString();
                    Rtc.ConsoleInput.SendText(text);
                    break;
            }
        }
        private static void Client_PacketReceived(Client sender, Client.PacketReceivedEventArgs e)
        {
            PacketType packetType = (PacketType)e.Reader.ReadInt16();
            switch (packetType)
            {
                case PacketType.Disconnect:
                    {
                        string msg = e.Reader.ReadString();
                        Console.WriteLine("Disconnected:" + msg);
                    }
                    break;
                case PacketType.Message:
                    {
                        ConsoleColor oldColor = Console.ForegroundColor;
                        ConsoleColor consoleColor = (ConsoleColor)e.Reader.ReadByte();
                        string msg = e.Reader.ReadString();
                        Console.ForegroundColor = consoleColor;

                        if (msg.EndsWith(": "))
                            Console.Write(msg);
                        else
                        Console.WriteLine(msg);

                        Console.ForegroundColor = oldColor;
                    }
                    break;
                case PacketType.MessageBuffer:
                    {
                        short length = e.Reader.ReadInt16();
                        ConsoleColor oldColor = Console.ForegroundColor;
                        for (int i = 0; i < length; i++)
                        {
                            Console.ForegroundColor = (ConsoleColor)e.Reader.ReadByte();
                            string msg = e.Reader.ReadString();
                            if (msg.EndsWith(": "))
                                Console.Write(msg);
                            else
                                Console.WriteLine(msg);
                        }
                        Console.ForegroundColor = oldColor;
                    }
                    break;
            }
        }
 private static void Client_ClientDisconnected(Client e)
 {
     Console.WriteLine("Connection lost. Attempting to reconnect in 5 seconds...");
     resetEvent.Set();
 }
        static void Main(string[] args)
        {
            if (!File.Exists("config.json"))
                MakeConfig();
            else
                config = Config.Load();

            Client client = new Client();
            try
            {
                client.PacketReceived += Client_PacketReceived;
                client.ClientDisconnected += Client_ClientDisconnected;
                Console.WriteLine("Connecting...");
                client.Connect(config.ServerIp, config.Port);
                Console.Clear();
                client.Send(new Packet((short)PacketType.Authenticate, (byte)0, Version.Major, Version.Minor, config.Username, config.Password));
            }
            catch
            {
                Console.WriteLine("Error connecting. Attempting to reconnect in 5 seconds...");
                resetEvent.Set();
            }

            if (client.Connected)
            {
                new Thread(() =>
                {
                    for (;;)
                    {
                        string input = Console.ReadLine();
                        if (!client.tcpClient.Connected)
                            break;
                        client.Send(new Packet((short)PacketType.Input, input));
                    }
                }).Start();
            }

            resetEvent.WaitOne();
            Thread.Sleep(5000);
            Console.Clear();
            Main(args);
            return;
        }
 public ConsoleClient(Client remoteClient)
 {
     RemoteClient = remoteClient;
     RemoteClient.PacketReceived += RemoteClient_PacketReceived;
 }