static void tick()
 {
     if (!sock.Connected)
     {
         Console.WriteLine("Connection failed.");
         thread.Abort();
         return;
     }
     while (sock.Available > 0)
     {
         // receive packets and respond if needed (keep-alive)
         int length = Util.readVarInt();
         int id     = Util.readVarInt();
         if (id == 0)
         {
             // keep-alive
             Packets.keepalive0x00();
         }
         else if (id == 2)
         {
             // chat message
             Console.WriteLine(Util.readString());
         }
         else
         {
             // everything else (this includes chunk updates and all that stuff we don't need in a console)
             if (length > 0 && length < 32768)
             {
                 byte[] buff = new byte[length + 1];
                 sock.Receive(buff, length, SocketFlags.None);
             }
         }
     }
 }
 public static void getServerInfo(string address, int port)
 {
     Packets.handshake0x00(address, port, 1);
     Packets.request0x00();
     Packets.response0x00();
     sock.Close();
 }
        // floods the console with small errors:

        /*
         * [13:55:18 INFO]: /127.0.0.1:65042 lost connection: Internal Exception: net.minecraft.util.io.netty.handler.codec.DecoderException: java.lang.RuntimeException: An internal error occured.
         *
         */

        // I once had this one occur, unfortunately I forgot the packet combination:

        /*
         * [15:17:33] [Server thread/WARN]: Failed to handle packet for /127.0.0.1:5437
         * java.lang.IllegalStateException: Unexpected hello packet
         * at net.minecraft.util.org.apache.commons.lang3.Validate.validState(Validate.java:826) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-11-g08fad7a-b3037jnks]
         * at net.minecraft.server.v1_7_R2.LoginListener.a(LoginListener.java:96) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-11-g08fad7a-b3037jnks]
         * at net.minecraft.server.v1_7_R2.PacketLoginInStart.a(SourceFile:33) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-11-g08fad7a-b3037jnks]
         * at net.minecraft.server.v1_7_R2.PacketLoginInStart.handle(SourceFile:10) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-11-g08fad7a-b3037jnks]
         * at net.minecraft.server.v1_7_R2.NetworkManager.a(NetworkManager.java:147) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-11-g08fad7a-b3037jnks]
         * at net.minecraft.server.v1_7_R2.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-11-g08fad7a-b3037jnks]
         * at net.minecraft.server.v1_7_R2.MinecraftServer.v(MinecraftServer.java:657) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-11-g08fad7a-b3037jnks]
         * at net.minecraft.server.v1_7_R2.DedicatedServer.v(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-11-g08fad7a-b3037jnks]
         * at net.minecraft.server.v1_7_R2.MinecraftServer.u(MinecraftServer.java:548) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-11-g08fad7a-b3037jnks]
         * at net.minecraft.server.v1_7_R2.MinecraftServer.run(MinecraftServer.java:459) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-11-g08fad7a-b3037jnks]
         * at net.minecraft.server.v1_7_R2.ThreadServerApplication.run(SourceFile:618) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-11-g08fad7a-b3037jnks]
         *
         */
        public static void floodErrors(string address, int port, int count)
        {
            for (int i = 0; i < count; i++)
            {
                Program.connect(address, port);
                Packets.handshake0x00(address, port, 2);
                Packets.handshake0x00(address, port, 2);
                Packets.loginStart0x00(randomUser(10));
            }
        }
 public static void login(string address, int port, string username, String password)
 {
     connect(address, port);
     Packets.handshake0x00(address, port, 2);
     Packets.loginStart0x00(username);
     if (Packets.loginResponse())
     {
         thread = new Thread(new ThreadStart(handleUpdates));
         thread.Start();
     }
 }
        // TODO test
        public static void floodPackets(string address, int port, int count)
        {
            Program.connect(address, port);
            for (int i = 0; i < count; i++)
            {
                if (Program.sock != null)
                {
                    if (!Program.sock.Connected)
                    {
                        Program.connect(address, port);
                    }
                }
                else
                {
                    Program.connect(address, port);
                }

                int x = r.Next(10);
                Console.Write(x);
                if (x == 1)
                {
                    Packets.handshake0x00(address, port, 1);
                }
                else if (x == 2)
                {
                    Packets.handshake0x00(address, port, 2);
                }
                else if (x == 3)
                {
                    Packets.chatMessage0x01(randomUser(30));
                }
                else if (x == 4)
                {
                    // might crash us
                    Packets.keepalive0x00();
                }
                else if (x == 5)
                {
                    Packets.loginStart0x00(randomUser(10));
                }
                else if (x == 6)
                {
                    Packets.request0x00();
                }
                else
                {
                    Packets.handshake0x00(address, port, 1);
                }
            }
        }
 // same ip, many users
 public static void floodUserLogins(string address, int port, int count)
 {
     Program.connect(address, port);
     Packets.handshake0x00(address, port, 2);
     Packets.loginStart0x00(randomUser(10));
 }
        static void Main(string[] args)
        {
            Console.WriteLine("Usage example: 5.104.104.45 25565 InstanceLabs password");
            Console.WriteLine("Leave password empty for offline mode.");
            bool   cmd      = false;
            String address  = "127.0.0.1";
            String portstr  = "25565";
            int    port     = 25565;
            String username = "";
            String password = "";

            if (args.Length > 3)
            {
                address  = args[0];
                portstr  = args[1];
                port     = Convert.ToInt32(portstr);
                username = args[2];
                password = args[3];
                cmd      = true;
            }
            else if (args.Length > 2 && args.Length < 4)
            {
                address  = args[0];
                portstr  = args[1];
                port     = Convert.ToInt32(portstr);
                username = args[2];
                password = "";
                cmd      = true;
            }
            bool menu = true;

            while (menu)
            {
                if (cmd)
                {
                    login(address, port, username, password);
                    while (sock.Connected)
                    {
                        // send message
                        Packets.chatMessage0x01(Console.ReadLine());
                    }
                    return;
                }
                Console.Write("Server > ");
                address = Console.ReadLine();
                if (address == "")
                {
                    address = "127.0.0.1";
                }
                Console.Write("Port > ");
                portstr = Console.ReadLine();
                if (portstr == "")
                {
                    portstr = "25565";
                }
                port = Convert.ToInt32(portstr);
                connect(address, port);
                getServerInfo(address, port);
                Console.Write("Connect? (y/n) ");
                if (Console.ReadLine().ToLower().StartsWith("y"))
                {
                    // connect
                    Console.Write("Username > ");
                    username = Console.ReadLine();
                    Console.Write("EMail (leave empty if you still have an unmigrated account) > ");
                    string email = Console.ReadLine();
                    Console.Write("Passord (leave empty for offline mode) > ");
                    password = Console.ReadLine();
                    Console.WriteLine(Util.loginSession(username, email, password));
                    login(address, port, username, password);
                    while (sock.Connected)
                    {
                        // send message
                        Packets.chatMessage0x01(Console.ReadLine());
                    }
                }
            }
        }