public static bool GetServerInfo(string serverIP, ref byte protocolversion, ref string version)
        {
            try
            {
                string host; int port;
                string[] sip = serverIP.Split(':');
                host = sip[0];

                if (sip.Length == 1)
                {
                    port = 25565;
                }
                else
                {
                    try
                    {
                        port = Convert.ToInt32(sip[1]);
                    }
                    catch (FormatException) { port = 25565; }
                }

                TcpClient tcp = new TcpClient(host, port);
                byte[] ping = new byte[2] { 0xfe, 0x01 };
                tcp.Client.Send(ping, SocketFlags.None);

                tcp.Client.Receive(ping, 0, 1, SocketFlags.None);
                if (ping[0] == 0xff)
                {
                    MinecraftCom ComTmp = new MinecraftCom();
                    ComTmp.setClient(tcp);
                    string result = ComTmp.readNextString();
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    //Console.WriteLine(result.Replace((char)0x00, ' '));
                    if (result.Length > 2 && result[0] == '§' && result[1] == '1')
                    {
                        string[] tmp = result.Split((char)0x00);
                        protocolversion = (byte)Int16.Parse(tmp[1]);
                        version = tmp[2];
                    }
                    else
                    {
                        protocolversion = (byte)39;
                        version = "B1.8.1 - 1.3.2";
                    }
                    Console.WriteLine("Server version : MC " + version + " (protocol v" + protocolversion + ").");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    return true;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    Console.WriteLine("Unexpected answer from the server (is that a Minecraft server ?)");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    return false;
                }
            }
            catch
            {
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.WriteLine("An error occured while attempting to connect to this IP.");
                Console.ForegroundColor = ConsoleColor.Gray;
                return false;
            }
        }
        /// <summary>
        /// Starts the main chat client, wich will login to the server using the MinecraftCom class.
        /// </summary>
        /// <param name="user">The chosen username of a premium Minecraft Account</param>
        /// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param>
        /// <param name="server_port">The server IP (serveradress or serveradress:port)/param>
        /// <param name="singlecommand">If set to true, the client will send a single command and then disconnect from the server</param>
        /// <param name="command">The text or command to send. Will only be sent if singlecommand is set to true.</param>
        private void StartClient(string user, string sessionID, string server_port, bool singlecommand, MinecraftCom handler, string command)
        {
            this.handler = handler;
            username = user;
            string[] sip = server_port.Split(':');
            host = sip[0];
            if (sip.Length == 1)
            {
                port = 25565;
            }
            else
            {
                try
                {
                    port = Convert.ToInt32(sip[1]);
                }
                catch (FormatException) { port = 25565; }
            }

            try
            {
                Console.WriteLine("Connecting...");
                client = new TcpClient(host, port);
                client.ReceiveBufferSize = 1024 * 1024;
                handler.setClient(client);
                byte[] token = new byte[1]; string serverID = "";
                if (handler.Handshake(user, sessionID, ref serverID, ref token, host, port))
                {
                    Console.WriteLine("Logging in...");

                    if (handler.FinalizeLogin())
                    {
                        //Single command sending
                        if (singlecommand)
                        {
                            handler.SendChatMessage(command);
                            Console.Write("Command ");
                            Console.ForegroundColor = ConsoleColor.DarkGray;
                            Console.Write(command);
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine(" sent.");
                            Thread.Sleep(5000);
                            handler.Disconnect("disconnect.quitting");
                            Thread.Sleep(1000);
                        }
                        else
                        {
                            Console.WriteLine("Server was successfuly joined.\nType '/quit' to leave the server.");

                            //Command sending thread, allowing user input
                            t_sender = new Thread(new ThreadStart(StartTalk));
                            t_sender.Name = "CommandSender";
                            t_sender.Start();

                            //Data receiving thread, allowing text receiving
                            t_updater = new Thread(new ThreadStart(Updater));
                            t_updater.Name = "PacketHandler";
                            t_updater.Start();
                        }
                    }
                    else
                    {
                        Console.WriteLine("Login failed.");
                        if (!singlecommand) { Program.ReadLineReconnect(); }
                    }
                }
                else
                {
                    Console.WriteLine("Invalid session ID.");
                    if (!singlecommand) { Program.ReadLineReconnect(); }
                }
            }
            catch (SocketException)
            {
                Console.WriteLine("Failed to connect to this IP.");
                if (AttemptsLeft > 0)
                {
                    ChatBot.LogToConsole("Waiting 5 seconds (" + AttemptsLeft + " attempts left)...");
                    Thread.Sleep(5000); AttemptsLeft--; Program.Restart();
                }
                else if (!singlecommand) { Console.ReadLine(); }
            }
        }
        public static bool GetServerInfo(string serverIP, ref int protocolversion, ref string version)
        {
            try
            {
                string host; int port;
                string[] sip = serverIP.Split(':');
                host = sip[0];

                if (sip.Length == 1)
                {
                    port = 25565;
                }
                else
                {
                    try
                    {
                        port = Convert.ToInt32(sip[1]);
                    }
                    catch (FormatException) { port = 25565; }
                }

                TcpClient tcp = new TcpClient(host, port);

                byte[] packet_id = getVarInt(0);
                byte[] protocol_version = getVarInt(4);
                byte[] server_adress_val = Encoding.UTF8.GetBytes(host);
                byte[] server_adress_len = getVarInt(server_adress_val.Length);
                byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port);
                byte[] next_state = getVarInt(1);
                byte[] packet = concatBytes(packet_id, protocol_version, server_adress_len, server_adress_val, server_port, next_state);
                byte[] tosend = concatBytes(getVarInt(packet.Length), packet);

                tcp.Client.Send(tosend, SocketFlags.None);

                byte[] status_request = getVarInt(0);
                byte[] request_packet = concatBytes(getVarInt(status_request.Length), status_request);

                tcp.Client.Send(request_packet, SocketFlags.None);

                MinecraftCom ComTmp = new MinecraftCom();
                ComTmp.setClient(tcp);
                if (ComTmp.readNextVarInt() > 0) //Read Response length
                {
                    if (ComTmp.readNextVarInt() == 0x00) //Read Packet ID
                    {
                        string result = ComTmp.readNextString(); //Get the Json data
                        if (result[0] == '{' && result.Contains("protocol\":") && result.Contains("name\":\""))
                        {
                            string[] tmp_ver = result.Split(new string[] { "protocol\":" }, StringSplitOptions.None);
                            string[] tmp_name = result.Split(new string[] { "name\":\"" }, StringSplitOptions.None);
                            if (tmp_ver.Length >= 2 && tmp_name.Length >= 2)
                            {
                                protocolversion = atoi(tmp_ver[1]);
                                version = tmp_name[1].Split('"')[0];
                                Console.ForegroundColor = ConsoleColor.DarkGray;
                                //Console.WriteLine(result); //Debug: show the full Json string
                                Console.WriteLine("Server version : " + version + " (protocol v" + protocolversion + ").");
                                Console.ForegroundColor = ConsoleColor.Gray;
                                return true;
                            }
                        }
                    }
                }
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.WriteLine("Unexpected answer from the server (is that a MC 1.7+ server ?)");
                Console.ForegroundColor = ConsoleColor.Gray;
                return false;
            }
            catch
            {
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.WriteLine("An error occured while attempting to connect to this IP.");
                Console.ForegroundColor = ConsoleColor.Gray;
                return false;
            }
        }