using System;
//using System.Collections.Generic;
using System.Text.RegularExpressions;
//using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Data;
using System.Threading;
//using System.Data.SqlClient;
//using MySql.Data.MySqlClient;
//using System.Net.Mail;
//using System.ComponentModel;
using System.Net;

using Ion;
using Ion.Manager;
using Ion.Sockets;
using Ion.Storage;

namespace Ion.Sockets
{
    class TcpSck
    {
        public int ID;
        private int maxcon;
        
        public TcpSck(int id)
        {
            ID = id;
        }
        public void run()
        {
            try
            {
                DatabaseClient dbClient = IonEnvironment.GetDatabase().GetClient();
                string svPort = IonEnvironment.Configuration["net.tcp.port"];
           		string svIP = IonEnvironment.Configuration["net.tcp.localip"];
           		maxcon = IonEnvironment.Configuration.TryParseInt32("net.tcp.maxcon");
			 

                System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                byte[] tmp_hello = enc.GetBytes(PaketHelper.Build_Send("HELLO" + (char)13).ToCharArray());

                IonEnvironment.GetLog().WriteDebug("<-- HELLO {13}");

                IonEnvironment.Users[ID].Sck.GetStream().Write(tmp_hello, 0, tmp_hello.Length);
 
                while (IonEnvironment.Users[ID].Sck.Connected)
                {
                    if (IonEnvironment.Users[ID].Sck.Available > 0)
                    {
                        byte[] len_info_byte = new byte[4];
                        Stream tmp = IonEnvironment.Users[ID].Sck.GetStream();
                        tmp.Read(len_info_byte, 0, 4);
                        string len_info_string = Encoding.ASCII.GetString(len_info_byte);

                        // Anti - HTTP - http://serverip:port/ ~~
                        /*if (len_info_string.Equals("GET ")) {
                        	Console.WriteLine("--> USING HTTP !");
                        	IonEnvironment.Users[ID].Sck.Close();
                            break;
                        }*/

                        int len_info_int = int.Parse(len_info_string);
                        
                        byte[] PaketData_byte = new byte[len_info_int];
                        tmp.Read(PaketData_byte, 0, len_info_int);
                        string PaketData_string = Encoding.ASCII.GetString(PaketData_byte);

                        string PaketData_string2 = PaketData_string;
                        IonEnvironment.GetLog().WriteDebug("--> " + PaketData_string.Replace("\r", " {13} "));

                        if (IonEnvironment.Users[ID].name != null && IonEnvironment.Users[ID].inroom == 0)
                        {
                            IonEnvironment.Users[ID].owner = 0;
                            try
                            {
                                IonEnvironment.Users[ID].inroom = dbClient.ReadInt32("SELECT inroom FROM users WHERE name = '" + IonEnvironment.Users[ID].name + "'");
                            }
                            catch
                            {
                                IonEnvironment.Users[ID].inroom = 0;
                            }
                        }

                        string[] header = PaketData_string.Split(' ');

                        switch (header[0].ToUpper())
                        {

                            // SocketServer

							//TODO: FINDUSER CarryDrink Moderator CRYFORHELP
                            
                            #region VERSIONCHECK ##
                            case "VERSIONCHECK":
                                {
                                    if (header[1] == "client002" || header[1] == "client003")
                                    {
                                        Send_Data("ENCRYPTION_OFF");
                                        Send_Data("SECRET_KEY" + (char)13 + "1337");
										//nz3vlinucwjwy4o66zznyolynl6b2pw1uhz8vj3ptwqdolmypmt0oinjqdkpl1xd69tyi9d0ahzviuww46znoylbp1h8jpwdlym0ijdp1d9y90h
                                    }
                                }
                                break;
                            #endregion

                            #region LOGIN *
                            case "LOGIN":
                                {
                            	
                          		
                                    try
                                    {
                                    	//dbClient.AddParamWithValue("name", header[1]);
                    					//dbClient.AddParamWithValue("password", header[2]);
                    					String name = DatabaseClient.FilterInjectionChars(header[1], false);
                    					String pass = DatabaseClient.FilterInjectionChars(header[2], false);
                    					
                                        string checkdata = dbClient.ReadString("SELECT name FROM users WHERE name = '" + name + "' AND password = '" + pass + "'");
              
                                        if (checkdata != null)
                                        {
                                        	DataRow dbRow = dbClient.ReadDataRow("SELECT name,password,credits,email,figure,customData,sex,has_special_rights,badge_type FROM users WHERE name = '" + name + "'");
                                            
											IonEnvironment.Users[ID].name = name; //(String)dbRow["name"];
                                            IonEnvironment.Users[ID].password = pass; //(String)dbRow["password"];
											IonEnvironment.Users[ID].credits = (int)dbRow["credits"];
                                            IonEnvironment.Users[ID].email = (String)dbRow["email"];
                                            IonEnvironment.Users[ID].figure = (String)dbRow["figure"];
                                            IonEnvironment.Users[ID].customData = (String)dbRow["customData"];
                                            IonEnvironment.Users[ID].sex = (String)dbRow["sex"];
                                            IonEnvironment.Users[ID].has_special_rights = (int)dbRow["has_special_rights"];
                                            IonEnvironment.Users[ID].badge_type = (String)dbRow["badge_type"];

                                            try
                                            {
                                                if (header[3] == "0")
                                                {
                                                    IonEnvironment.Users[ID].server = 1;

                                                    #region SHIT!
                                                    Send_Data("OBJECTS WORLD 0 lobby_a" + (char)13 +
                                                        "f90 flower1 9 0 7 0" + (char)13 +
                                                        "S110 chairf2b 11 0 7 4" + (char)13 +
                                                        "s120 chairf2 12 0 7 4" + (char)13 +
                                                        "t130 table1 13 0 7 2" + (char)13 +
                                                        "S140 chairf2b 14 0 7 4" + (char)13 +
                                                        "s150 chairf2 15 0 7 4" + (char)13 +
                                                        "w160 watermatic 16 0 7 4" + (char)13 +
                                                        "T92 telkka 9 2 7 2" + (char)13 +
                                                        "f93 flower1 9 3 7 0" + (char)13 +
                                                        "Z113 chairf2d 11 3 7 0" + (char)13 +
                                                        "s123 chairf2 12 3 7 0" + (char)13 +
                                                        "t133 table1 13 3 7 2" + (char)13 +
                                                        "Z143 chairf2d 14 3 7 0" + (char)13 +
                                                        "s153 chairf2 15 3 7 0" + (char)13 +
                                                        "f124 flower1 12 4 3 0" + (char)13 +
                                                        "f164 flower1 16 4 3 0" + (char)13 +
                                                        "S07 chairf2b 0 7 3 4" + (char)13 +
                                                        "s17 chairf2 1 7 3 4" + (char)13 +
                                                        "Z010 chairf2d 0 10 3 0" + (char)13 +
                                                        "s110 chairf2 1 10 3 0" + (char)13 +
                                                        "r2112 roommatic 21 12 1 4" + (char)13 +
                                                        "r2212 roommatic 22 12 1 4" + (char)13 +
                                                        "r2312 roommatic 23 12 1 4" + (char)13 +
                                                        "r2412 roommatic 24 12 1 4" + (char)13 +
                                                        "S014 chairf2b 0 14 3 4" + (char)13 +
                                                        "s114 chairf2 1 14 3 4" + (char)13 +
                                                        "w1314 watermatic 13 14 1 2" + (char)13 +
                                                        "w1215 watermatic 12 15 1 4" + (char)13 +
                                                        "c1916 chairf1 19 16 1 4" + (char)13 +
                                                        "C2116 table2c 21 16 1 2" + (char)13 +
                                                        "c2316 chairf1 23 16 1 4" + (char)13 +
                                                        "Z017 chairf2d 0 17 3 0" + (char)13 +
                                                        "s117 chairf2 1 17 3 0" + (char)13 +
                                                        "D2117 table2b 21 17 1 2" + (char)13 +
                                                        "c1918 chairf1 19 18 1 0" + (char)13 +
                                                        "d2118 table2 21 18 1 2" + (char)13 +
                                                        "c2318 chairf1 23 18 1 0" + (char)13 +
                                                        "S721 chairf2b 7 21 2 2" + (char)13 +
                                                        "z722 chairf2c 7 22 2 2" + (char)13 +
                                                        "z723 chairf2c 7 23 2 2" + (char)13 +
                                                        "z724 chairf2c 7 24 2 2" + (char)13 +
                                                        "s725 chairf2 7 25 2 2" + (char)13 +
                                                        "t726 table1 7 26 2 2" + (char)13 +
                                                        "e1026 flower2 10 26 1 2" + (char)13);
                                                    #endregion

                                                    string RoomMap = dbClient.ReadString("SELECT heightmap FROM heightmap WHERE model = 'lobby_a'");
                                                    IonEnvironment.Users[ID].roomcache = RoomMap;

                                                    Send_Data("HEIGHTMAP" + RoomMap.Replace(" ", "\r"));
                                                    Send_Data("USERS" + (char)13 + " " + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].figure.Replace("figure=", "") + " 3 5 0 " + IonEnvironment.Users[ID].customData);
                                                    IonEnvironment.Users[ID].userx = 12;
                                                    IonEnvironment.Users[ID].usery = 27;
                                                    Send_Data("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " 12,27,1,0,0/mod 0/");
                                                }
                                            }
                                            catch (IndexOutOfRangeException)
                                            {
                                                // no header[3] exist -> private room loads
                                            }
                                        }
                                        else
                                        {
                                            Send_Data("ERROR: login incorrect");
                                        }
                                    }
                                    catch (NullReferenceException)
                                    {
                                        Send_Data("ERROR: login incorrect");
                                    }
                                }
                                break;
                            #endregion

                            #region INFORETRIEVE *
                            case "INFORETRIEVE":
                                {
                                    try
                                    {
                                        String name = DatabaseClient.FilterInjectionChars(header[1], false);
                    					String pass = DatabaseClient.FilterInjectionChars(header[2], false);
                    					
                                        string checkdata = dbClient.ReadString("SELECT name FROM users WHERE name = '" + name + "' AND password = '" + pass + "'");
                                        
                                        if (checkdata != null)
                                        {
                                        	DataRow dbRow = dbClient.ReadDataRow("SELECT name,password,credits,email,figure,customData,sex,has_special_rights,badge_type FROM users WHERE name = '" + name + "'");
                                            
                                      	
                                            IonEnvironment.Users[ID].name = name; //header[1];
                                            IonEnvironment.Users[ID].password = pass; //header[2];
                                            IonEnvironment.Users[ID].credits = (int)dbRow["credits"];
                                            IonEnvironment.Users[ID].email = (String)dbRow["email"];
                                            IonEnvironment.Users[ID].figure = (String)dbRow["figure"];
                                            IonEnvironment.Users[ID].customData = (String)dbRow["customData"];
                                            IonEnvironment.Users[ID].sex = (String)dbRow["sex"];
                                            IonEnvironment.Users[ID].has_special_rights = (int)dbRow["has_special_rights"];
                                            IonEnvironment.Users[ID].badge_type = (String)dbRow["name"];

                                            Send_Data(
                                                "USEROBJECT" + (char)13 +
                                                "name=" + IonEnvironment.Users[ID].name + (char)13 +
                                                "email=" + IonEnvironment.Users[ID].email + (char)13 +
                                                "figure=" + IonEnvironment.Users[ID].figure + (char)13 +
                                                "birthday=" + IonEnvironment.Users[ID].birthday + (char)13 +
                                                "phonenumber=" + IonEnvironment.Users[ID].phonenumber + (char)13 +
                                                "customData=" + IonEnvironment.Users[ID].customData + (char)13 +
                                                "had_read_agreement=" + IonEnvironment.Users[ID].had_read_agreement + (char)13 +
                                                "sex=" + IonEnvironment.Users[ID].sex + (char)13 +
                                                "country=" + IonEnvironment.Users[ID].country + (char)13 +
                                                "has_special_rights=" + IonEnvironment.Users[ID].has_special_rights + (char)13 +
                                                "badge_type=" + IonEnvironment.Users[ID].badge_type + (char)13);
                                        }
                                    }
                                    catch (NullReferenceException)
                                    {
                                        Send_Data("ERROR: login incorrect");
                                    }
                                }
                                break;
                            #endregion

                            #region INITUNITLISTENER ##
                            case "INITUNITLISTENER":
                                {
                                    string RoomData = "";
                                    DataTable Data = null;

                                    Data = dbClient.ReadDataTable("SELECT * FROM pubs");

                                    if (Data != null)
                                    {
                                        foreach (DataRow Row in Data.Rows)
                                        {
                                            RoomData = RoomData + (string)Row["name"] + "," + (int)Row["now_in"] + "," + (int)Row["max_in"] + "," + svIP + "/" + svIP + "," + svPort + "," + (string)Row["name"] + (char)9 + (string)Row["mapname"] + "," + (int)Row["now_in"] + "," + (int)Row["max_in"] + "," + (string)Row["heightmap"] + (char)13;
                                        }
                                    }

                                    Send_Data("ALLUNITS" + (char)13 + RoomData);
                                }
                                break;
                            #endregion

                            #region SEARCHBUSYFLATS *#
                            case "SEARCHBUSYFLATS":
                                {
                                    string RoomData = "";
                                    DataTable Data = null;

                                    Data = dbClient.ReadDataTable("SELECT * FROM rooms ORDER BY inroom DESC");

                                    if (Data != null)
                                    {
                                        foreach (DataRow Row in Data.Rows)
                                        {
                                            RoomData = RoomData + (char)13 + (int)Row["id"] + "/" + (string)Row["name"] + "/" + (string)Row["owner"] + "/" + (string)Row["door"] + "/" + (string)Row["pass"] + "/" + (string)Row["floor"] + "/" + svIP + "/" + svIP + "/" + svPort + "/" + (int)Row["inroom"] + "/null/" + (string)Row["desc"] + "";
                                        }
                                    }

                                    Send_Data("BUSY_FLAT_RESULTS 1" + RoomData);
                                }
                                break;
                            #endregion

                            #region GETCREDITS ##
                            case "GETCREDITS":
                                {
                                    Send_Data("WALLETBALANCE" + (char)13 + IonEnvironment.Users[ID].credits);
                                    Send_Data("MESSENGERSMSACCOUNT" + (char)13 + "noaccount");
                                    Send_Data("MESSENGERREADY " + (char)13);
                                }
                                break;
                            #endregion

                            // Register desactivé sur le client >> risque de doublons !!
                            /*
                            #region REGISTER *
                            case "REGISTER":
                                {
                                    string[] PacketData = PaketData_string.Split((char)13);
                                    string RegUserName = Regex.Split(PacketData[0], "=")[1];
                                    string RegUserPass = Regex.Split(PacketData[1], "=")[1];
                                    string RegUserMail = Regex.Split(PacketData[2], "=")[1];
                                    string RegUserFigure = PacketData[3].PadRight(PacketData[3].Length - 7).Replace("figure=", "");
                                    string RegUserDate = Regex.Split(PacketData[5], "=")[1];
                                    string RegUserPhone = Regex.Split(PacketData[6], "=")[1];
                                    string RegUserMission = Regex.Split(PacketData[7], "=")[1];
                                    string RegUserAgree = Regex.Split(PacketData[8], "=")[1];
                                    string RegUserSex = Regex.Split(PacketData[9], "=")[1];
                                    string RegUserCity = Regex.Split(PacketData[10], "=")[1];

                                    dbClient.ExecuteQuery("INSERT INTO users (`id`, `name`, `password`, `credits`, `email`, `figure`, `birthday`, `phonenumber`, `customData`, `had_read_agreement`, `sex`, `country`, `has_special_rights`, `badge_type`) VALUES (NULL, '" + RegUserName + "', '" + RegUserPass + "', '1337', '" + RegUserMail + "', '" + RegUserFigure + "', '" + RegUserDate + "', '" + RegUserPhone + "', '" + RegUserMission + "', '" + RegUserAgree + "', '" + RegUserSex + "', '" + RegUserCity + "', '0', '0');");
                                }
                                break;
                            #endregion
							*/

							#region APPROVENAME
							case "APPROVENAME":
								{
									Send_Data("BADNAME"); // if exist then badname
								}
							break;
							#endregion

                            #region UPDATE #
                            case "UPDATE":
                                {
                                    string[] PacketData = PaketData_string.Split((char)13);
                                    string UpdUserName = Regex.Split(PacketData[0], "=")[1];
                                    string UpdUserPass = Regex.Split(PacketData[1], "=")[1];
                                    string UpdUserMail = Regex.Split(PacketData[2], "=")[1];
                                    string UpdUserFigure = PacketData[3].PadRight(PacketData[3].Length - 7).Replace("figure=", "");
                                    string UpdUserDate = Regex.Split(PacketData[5], "=")[1];
                                    string UpdUserPhone = Regex.Split(PacketData[6], "=")[1];
                                    string UpdUserMission = Regex.Split(PacketData[7], "=")[1];
                                    string UpdUserAgree = Regex.Split(PacketData[8], "=")[1];
                                    string UpdUserSex = Regex.Split(PacketData[9], "=")[1];
                                    string UpdUserCity = Regex.Split(PacketData[10], "=")[1];

                                    dbClient.ExecuteQuery("UPDATE users SET password =  '" + UpdUserPass + "', email = '" + UpdUserMail + "', figure = '" + UpdUserFigure + "', birthday = '" + UpdUserDate + "', phonenumber = '" + UpdUserPhone + "', had_read_agreement = '" + UpdUserAgree + "', country = '" + UpdUserCity + "', sex = '" + UpdUserSex + "', customData = '" + UpdUserMission + "' WHERE name = '" + UpdUserName + "' LIMIT 1");
                                }
                                break;
                            #endregion

                            #region SEARCHFLATFORUSER *
                            case "SEARCHFLATFORUSER":
                                {
                                    string GetFromUser = PaketData_string.Split('/')[1];
                                    string RoomData = "";
                                    DataTable Data = null;

                                    Data = dbClient.ReadDataTable("SELECT * FROM rooms WHERE owner = '" + GetFromUser + "'");

                                    if (Data != null)
                                    {
                                        foreach (DataRow Row in Data.Rows)
                                        {
                                            RoomData = RoomData + (char)13 + (int)Row["id"] + "/" + (string)Row["name"] + "/" + (string)Row["owner"] + "/" + (string)Row["door"] + "/" + (string)Row["pass"] + "/" + (string)Row["floor"] + "/83.117.80.215/83.117.80.215/37120/0/null/" + (string)Row["desc"] + "";
                                        }
                                    }

                                    Send_Data("BUSY_FLAT_RESULTS 1" + RoomData);
                                }
                                break;
                            #endregion

                            #region UINFO_MATCH *
                            case "UINFO_MATCH":
                                {
                                    string[] UInfo = PaketData_string.Split('/');

                                    DataTable Data = null;

                                    Data = dbClient.ReadDataTable("SELECT customData,figure,sex FROM users WHERE name = '" + UInfo[1] + "'");

                                    if (Data != null)
                                    {
                                        foreach (DataRow Row in Data.Rows) {
                                            Send_Data("MEMBERINFO MESSENGER" + (char)13 + UInfo[1] + (char)13 + (string)Row["customData"] + (char)13 + "" + (char)13 + "0" + (char)13 + (string)Row["figure"].ToString().Replace("figure=", "") + (string)Row["sex"]);
                                        }
                                    }
                                    else
                                    {
 										Send_Data("NOSUCHUSER MESSENGER" + (char)13 + UInfo[1]);
                                    }
                                }
                                break;
                            #endregion

                            #region GETFLATINFO *
                            case "GETFLATINFO":
                                {
                                    string[] GetFlatID = PaketData_string.Split('/');
                                    Send_Data("SETFLATINFO" + (char)13 + "/1/");
                                }
                                break;
                            #endregion

                            #region DELETEFLAT ##
                            case "DELETEFLAT":
                                {
                                    string[] GetFlatID = PaketData_string.Split('/');
                                    dbClient.ExecuteQuery("DELETE FROM rooms WHERE id = '" + GetFlatID[1] + "'");
                                }
                                break;
                            #endregion

                            #region GET_FAVORITE_ROOMS *
                            case "GET_FAVORITE_ROOMS":
                                {
                                    DataTable Data = null;
                                    string RoomData = "";

                                    Data = dbClient.ReadDataTable("SELECT * FROM favrooms WHERE user = '" + header[1] + "'");

                                    if (Data != null)
                                    {
                                        foreach (DataRow Row in Data.Rows)
                                        {
                                            DataTable Data2 = null;

                                            Data2 = dbClient.ReadDataTable("SELECT * FROM rooms WHERE id = '" + (int)Row["roomid"] + "' LIMIT 1");

                                            if (Data2 != null)
                                            {
                                                foreach (DataRow Row2 in Data2.Rows)
                                                {
                                                    RoomData = RoomData + (char)13 + (int)Row2["id"] + "/" + (string)Row2["name"] + "/" + (string)Row2["owner"] + "/" + (string)Row2["door"] + "/" + (string)Row2["pass"] + "/" + (string)Row2["floor"] + "/83.117.80.215/83.117.80.215/37120/" + (int)Row2["inroom"] + "/null/" + (string)Row2["desc"] + "";
                                                }
                                            }
                                        }
                                    }

                                    Send_Data("BUSY_FLAT_RESULTS 1" + RoomData);
                                }
                                break;
                            #endregion

                            #region ADD_FAVORITE_ROOM *
                            case "ADD_FAVORITE_ROOM":
                                {
                                    dbClient.ExecuteQuery("INSERT INTO favrooms VALUES (NULL, '" + IonEnvironment.Users[ID].name + "', '" + header[1] + "')");
                                }
                                break;
                            #endregion

                            #region DEL_FAVORITE_ROOM #
                            case "DEL_FAVORITE_ROOM":
                                {
                                    dbClient.ExecuteQuery("DELETE FROM favrooms WHERE roomid = '" + header[1] + "' LIMIT 1");
                                }
                                break;
                            #endregion

                            #region SEARCHFLAT *
                            case "SEARCHFLAT":
                                {
                                    string[] SearchLike = PaketData_string.Split('/');

                                    DataTable Data = null;
                                    string RoomData = "";

                                    Data = dbClient.ReadDataTable("SELECT * FROM rooms WHERE owner LIKE '" + SearchLike[1] + "' OR name LIKE '" + SearchLike[1] + "'");

                                    if (Data != null)
                                    {
                                        foreach (DataRow Row in Data.Rows)
                                        {
                                            RoomData = RoomData + (char)13 + (int)Row["id"] + "/" + (string)Row["name"] + "/" + (string)Row["owner"] + "/" + (string)Row["door"] + "/" + (string)Row["pass"] + "/" + (string)Row["floor"] + "/83.117.80.215/83.117.80.215/37120/" + (int)Row["inroom"] + "/null/" + (string)Row["desc"] + "";
                                        }
                                    }

                                    Send_Data("BUSY_FLAT_RESULTS 1" + RoomData);
                                }
                                break;
                            #endregion
				
							// TO MUCH FAIL !!
                            /* #region SEND_USERPASS_TO_EMAIL *
                            case "SEND_USERPASS_TO_EMAIL":
                                {
                                    string rlemail = dbClient.ReadString("SELECT email FROM users WHERE name = '" + header[1] + "' AND email = '" + header[2] + "'");
                                    string rlpasswd = dbClient.ReadString("SELECT password FROM users WHERE name = '" + header[1] + "' AND email = '" + header[2] + "'");

                                    if (rlemail != null)
                                    {
                                        var client = new SmtpClient("smtp.gmail.com", 587)
                                        {
                                            Credentials = new NetworkCredential("prjOwnage@gmail.com", "lolwutpwn"),
                                            EnableSsl = true
                                        };
                                        client.Send("prjOwnage@gmail.com", rlemail, "Habbo Hotel", "Hey, " + header[1] + "\nYou have clicked on the \"Forgotten your password??\" button,\n\nYour password is: " + rlpasswd);
                                    }
                                }
                                break;
                            #endregion */

                            //P&P Server
                            #region TRYFLAT #
                            case "TRYFLAT":
                                {
                                    int gotoroom = int.Parse(PaketData_string.Split('/')[1]);
                                    if (IonEnvironment.Users[ID].inroom != 0)
                                    {
                                        dbClient.ExecuteQuery("UPDATE rooms SET inroom = inroom - 1 WHERE id = '" + IonEnvironment.Users[ID].inroom + "'");
                                        IonEnvironment.Users[ID].owner = 0;
                                    }
                                    dbClient.ExecuteQuery("UPDATE users SET inroom = '" + gotoroom + "' WHERE name = '" + IonEnvironment.Users[ID].name + "'");
                                    dbClient.ExecuteQuery("UPDATE rooms SET inroom = inroom + 1 WHERE id = '" + gotoroom + "'");
                                    IonEnvironment.Users[ID].inroom = gotoroom;

                                    try
                                    {
                                        DataTable Data = null;
                                        bool RightPasswd = false;

                                        Data = dbClient.ReadDataTable("SELECT * FROM rooms WHERE id = '" + gotoroom + "'");

                                        if (Data != null)
                                        {
                                            foreach (DataRow Row in Data.Rows)
                                            {
                                                if ((string)Row["owner"] != IonEnvironment.Users[ID].name && IonEnvironment.Users[ID].has_special_rights != 1)
                                                {
                                                    if ((string)Row["pass"] == PaketData_string.Split('/')[2])
                                                    {
                                                        RightPasswd = true;
                                                    }
                                                }
                                                else
                                                {
                                                    RightPasswd = true;
                                                }
                                            }
                                        }

                                        if (RightPasswd == false)
                                        {
                                            IonEnvironment.Users[ID].Sck.Close();
                                            break;
                                        }
                                    }
                                    catch
                                    {
                                        // No password on the room,...
                                    }

                                    Send_Data("FLAT_LETIN");
                                }
                                break;
                            #endregion

                            #region GOTOFLAT **
                            case "GOTOFLAT":
                                {
                                    int gotoroom = int.Parse(PaketData_string.Split('/')[1]);

                                    DataTable Data = null;

                                    Data = dbClient.ReadDataTable("SELECT * FROM rooms WHERE id = '" + gotoroom + "' LIMIT 1");

                                    if (Data == null)
                                    {
                                        return;
                                    }

                                    foreach (DataRow Row in Data.Rows)
                                    {
                                        Send_Data("ROOM_READY" + (char)13 + (string)Row["desc"]);
                                        Send_Data("FLATPROPERTY" + (char)13 + "wallpaper/" + (int)Row["space_w"] + (char)13);
                                        Send_Data("FLATPROPERTY" + (char)13 + "floor/" + (int)Row["space_f"] + (char)13);

                                        if ((string)Row["owner"].ToString().ToLower() == IonEnvironment.Users[ID].name.ToLower())
                                        {
                                            IonEnvironment.Users[ID].owner = 1;
                                            Send_Data("YOUAREOWNER");
                                        }

                                        DataTable Data02 = null;

                                        Data02 = dbClient.ReadDataTable("SELECT * FROM rights WHERE user = '" + IonEnvironment.Users[ID].name + "' LIMIT 1");

                                        IonEnvironment.Users[ID].rights = 0;

                                        if (Data02 != null)
                                        {
                                            foreach (DataRow Row02 in Data02.Rows)
                                            {
                                                IonEnvironment.Users[ID].rights = 1;
                                                Send_Data("YOUARECONTROLLER");
                                            }
                                        }

                                        string RoomMap = dbClient.ReadString("SELECT heightmap FROM heightmap WHERE model = '" + (string)Row["model"] + "'");
                                        IonEnvironment.Users[ID].roomcache = RoomMap;

                                        Send_Data("HEIGHTMAP" + (char)13 + RoomMap.Replace(" ", "\r"));
                                        Send_Data("OBJECTS WORLD 0 " + (string)Row["model"]);

                                        DataTable RoomItemData = null;
                                        string RoomTotalData = "";

                                        RoomItemData = dbClient.ReadDataTable("SELECT * FROM roomitems WHERE roomid = '" + gotoroom + "'");

                                        if (RoomItemData != null)
                                        {
                                            foreach (DataRow RowXX in RoomItemData.Rows)
                                            {
                                                int zero = (int)RowXX["id"];
                                                string zstring = "00000000000";
                                                int j = 0;
                                                while (j < zero.ToString().Length)
                                                {
                                                    zstring = zstring + "0";
                                                    j++;
                                                }

                                                RoomTotalData = RoomTotalData + (char)13 + zstring + (int)RowXX["id"] + "," + (string)RowXX["textname"] + " " + (int)RowXX["x"] + " " + (int)RowXX["y"] + " " + (string)RowXX["size"] + " " + (int)RowXX["rotate"] + " 0.0 " + (string)RowXX["rgb"] + " /" + (string)RowXX["itemname"] + "/" + (string)RowXX["itemdesc"] + "/";
                                            }
                                        }

                                        Send_Data("ACTIVE_OBJECTS " + RoomTotalData);
                                        Send_Data("ITEMS " + (char)13 + "6663" + (char)9 + "poster" + (char)9 + " " + (char)9 + "frontwall 6.9999,6.3500/21");

                                        DataTable Data2 = null;

                                        Data2 = dbClient.ReadDataTable("SELECT name,figure,customData FROM users WHERE inroom = '" + gotoroom + "' AND name != '" + IonEnvironment.Users[ID].name + "'");

                                        if (Data == null)
                                        {
                                            return;
                                        }

                                        foreach (DataRow Row2 in Data2.Rows)
                                        {
                                            Send_Data("USERS" + (char)13 + " " + (string)Row2["name"] + " " + (string)Row2["figure"].ToString().Replace("figure=", "") + " 3 5 0 " + (string)Row2["customData"]);
                                        }

                                        if (IonEnvironment.Users[ID].owner == 1 || IonEnvironment.Users[ID].rights == 1)
                                        {
                                            Send_Data_All("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " 0,0,99,2,2/flatctrl useradmin/mod 0/");
                                            Send_Data_Room("USERS" + (char)13 + " " + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].figure.Replace("figure=", "") + " 3 5 0 " + IonEnvironment.Users[ID].customData);
                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " 3,5,0,2,2/flatctrl useradmin/mod 0/");
                                        }
                                        else
                                        {
                                            Send_Data_All("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " 0,0,99,2,2/mod 0/");
                                            Send_Data_Room("USERS" + (char)13 + " " + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].figure.Replace("figure=", "") + " 3 5 0 " + IonEnvironment.Users[ID].customData);
                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " 3,5,0,2,2/mod 0/");
                                        }
                                        IonEnvironment.Users[ID].userx = 3;
                                        IonEnvironment.Users[ID].usery = 5;
                                    }
                                }
                                break;
                            #endregion

                            #region SHOUT *
                            case "SHOUT":
                                {
                                    DataTable Data = null;
                                    string msg = PaketData_string.Replace("SHOUT ", "").Replace("'", "").Replace("\"", "").Replace("#", "");

                                    // chat cmds

                                    Data = dbClient.ReadDataTable("SELECT word,filter FROM wordfilter");

                                    if (Data == null)
                                    {
                                        return;
                                    }

                                    foreach (DataRow Row in Data.Rows)
                                    {
                                        msg = msg.Replace((string)Row["word"], (string)Row["filter"]);
                                    }

                                    Send_Data_Room("SHOUT" + (char)13 + IonEnvironment.Users[ID].name + " " + msg);

                                    dbClient.ExecuteQuery("INSERT INTO chatloggs VALUES (NULL, '" + IonEnvironment.Users[ID].name + "',  '" + msg + "', '" + IonEnvironment.Users[ID].inroom + "', 'shout')");
                                }
                                break;
                            #endregion

                            #region CHAT *
                            case "CHAT":
                                {
                                    DataTable Data = null;
                                    string msg = PaketData_string.Replace("CHAT ", "").Replace("'", "").Replace("\"", "").Replace("#", "");

                                    //if (ChatMessage.StartsWith("/"))
									if (msg.Substring(0, 1) == ":")
                                    {
                                        if (IonEnvironment.Users[ID].has_special_rights == 1)
                                        {

											switch (msg.Substring(1).Split(' ')[0]){

												case "teleport":
													if (IonEnvironment.Users[ID].teleport == 1)
													{
														IonEnvironment.Users[ID].teleport = 0;
													}
													else
													{
														IonEnvironment.Users[ID].moonwalk = 0;
														IonEnvironment.Users[ID].teleport = 1;
													}
													break;

												case "action":
													Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " +	IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + ",0,2,2/" +	msg.Split(' ')[1] + "/");
												break;

												case "moonwalk":
													if (IonEnvironment.Users[ID].moonwalk == 1)
													{
														IonEnvironment.Users[ID].moonwalk = 0;
														//Send_Data("SYSTEMBROADCAST" + (char)13 + "MoonWalk disabled!");
													}
													else
													{
														IonEnvironment.Users[ID].teleport = 0;
														IonEnvironment.Users[ID].moonwalk = 1;
														//Send_Data("SYSTEMBROADCAST" + (char)13 + "MoonWalk enabled!");
													}
												break;

												case "sit":
													Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + ",0,2,2/sit 1/");
												break;

												case "lay" :
													Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + ",0,2,2/lay 1/");
												break;

												

											}

											/* 
											   "swim 7/" "swim XY/"
												talk
												
												  if flatadmin = True then
													afs = "flatctrl useradmin/"
												afs = afs + "mod " + GetVal(username,"badge_type") + "/"

											*/
                                           /*
                                             if (ChatMessage.StartsWith("/cleanhand"))
                                            {
                                                dbClient.ExecuteQuery("DELETE FROM handitems WHERE user = '" + header[2] + "'");
                                            }

                                            if (ChatMessage.StartsWith("/action"))
                                            {
                                                Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + ",0,2,2/" + header[2] + "/");
                                            }

											*/
                                            
                                           /* #region /credits
                                            if (ChatMessage.StartsWith("/credits"))
                                            {
                                                dbClient.ExecuteQuery("UPDATE users SET credits = credits + " + header[3] + " WHERE name = '" + header[2] + "'");
                                            }
                                            #endregion 

                                            #region /changename
                                            if (ChatMessage.StartsWith("/changename"))
                                            {
                                                IonEnvironment.Users[ID].name = header[2];
                                            }
                                            #endregion */
                                        }

                                    }

                                    Data = dbClient.ReadDataTable("SELECT * FROM wordfilter");

                                    if (Data == null)
                                    {
                                        return;
                                    }

                                    foreach (DataRow Row in Data.Rows)
                                    {
                                        msg = msg.Replace((string)Row["word"], (string)Row["filter"]);
                                    }

                                    Send_Data_Room("CHAT" + (char)13 + IonEnvironment.Users[ID].name + " " + msg);

                                    dbClient.ExecuteQuery("INSERT INTO chatloggs VALUES (NULL, '" + IonEnvironment.Users[ID].name + "',  '" + msg + "', '" + IonEnvironment.Users[ID].inroom + "', 'chat')");
                                }
                                break;
                            #endregion

                            #region WHISPER *
                            case "WHISPER":
                                {
                                    DataTable Data = null;
                                    string msg = PaketData_string.Replace("WHISPER ", "").Replace("'", "").Replace("\"", "").Replace("#", "");

									// chat cmds

                                    Data = dbClient.ReadDataTable("SELECT * FROM wordfilter");


                                    if (Data == null)
                                    {
                                        return;
                                    }

                                    foreach (DataRow Row in Data.Rows)
                                    {
                                        msg = msg.Replace((string)Row["word"], (string)Row["filter"]);
                                    }

                                    Send_Data_Room("WHISPER" + (char)13 + IonEnvironment.Users[ID].name + " " + msg);

                                    dbClient.ExecuteQuery("INSERT INTO chatloggs VALUES (NULL, '" + IonEnvironment.Users[ID].name + "',  '" + msg + "', '" + IonEnvironment.Users[ID].inroom + "', 'whisper')");

                                }
                                break;
                            #endregion

                            #region STATUSOK #
                            case "STATUSOK":
                                {
                                    Send_Data("OK");
                                }
                                break;
                            #endregion

                            #region MOVE *
                            case "MOVE":
                                {
                                    try
                                    {
                                        int ToX = int.Parse(header[1]);
                                        int ToY = int.Parse(header[2]);

                                        string OwnData = "";

                                        if (IonEnvironment.Users[ID].owner == 1 || IonEnvironment.Users[ID].rights == 1)
                                        {
                                            OwnData = "flatctrl useradmin/";
                                        }

                                        if (IonEnvironment.Users[ID].teleport == 1)
                                        {
                                            string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[ToY].Substring(ToX, 1);
                                            string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[ToY].Substring(ToX, 1);

                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",0,0 /" + OwnData + "");
                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + ToX + "," + ToY + "," + RoomHeightNext + ",2,2/" + OwnData + "");
                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + ToX + "," + ToY + "," + RoomHeightNext + ",2,2/" + OwnData + "");
                                            IonEnvironment.Users[ID].userx = ToX;
                                            IonEnvironment.Users[ID].usery = ToY;
                                        }
                                        else
                                        {
                                            while (IonEnvironment.Users[ID].userx != ToX || IonEnvironment.Users[ID].usery != ToY)
                                            {
                                                if (IonEnvironment.Users[ID].moonwalk == 1)
                                                {
                                                    #region > ToX > ToY
                                                    while (IonEnvironment.Users[ID].userx > ToX && IonEnvironment.Users[ID].usery > ToY)
                                                    {
                                                        int NewX = IonEnvironment.Users[ID].userx - 1;
                                                        int NewY = IonEnvironment.Users[ID].usery - 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[NewY].Substring(NewX, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",1,1/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",1,1/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].userx = NewX;
                                                        IonEnvironment.Users[ID].usery = NewY;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 7;
                                                        IonEnvironment.Users[ID].userry = 7;
                                                    }
                                                    #endregion

                                                    #region > ToX < ToY
                                                    while (IonEnvironment.Users[ID].userx > ToX && IonEnvironment.Users[ID].usery < ToY)
                                                    {
                                                        int NewX = IonEnvironment.Users[ID].userx - 1;
                                                        int NewY = IonEnvironment.Users[ID].usery + 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[NewY].Substring(NewX, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",3,3/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",3,3/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].userx = NewX;
                                                        IonEnvironment.Users[ID].usery = NewY;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 5;
                                                        IonEnvironment.Users[ID].userry = 5;
                                                    }
                                                    #endregion

                                                    #region < ToX > ToY
                                                    while (IonEnvironment.Users[ID].userx < ToX && IonEnvironment.Users[ID].usery > ToY)
                                                    {
                                                        int NewX = IonEnvironment.Users[ID].userx + 1;
                                                        int NewY = IonEnvironment.Users[ID].usery - 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[NewY].Substring(NewX, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",7,7/mv " + NewX + "," + NewY + "," + RoomHeightNext + "" + OwnData + "/dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",7,7/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].userx = NewX;
                                                        IonEnvironment.Users[ID].usery = NewY;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 1;
                                                        IonEnvironment.Users[ID].userry = 1;
                                                    }
                                                    #endregion

                                                    #region < ToX < ToY
                                                    while (IonEnvironment.Users[ID].userx < ToX && IonEnvironment.Users[ID].usery < ToY)
                                                    {
                                                        int NewX = IonEnvironment.Users[ID].userx + 1;
                                                        int NewY = IonEnvironment.Users[ID].usery + 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[NewY].Substring(NewX, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",5,5/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",5,5/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].userx = NewX;
                                                        IonEnvironment.Users[ID].usery = NewY;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 3;
                                                        IonEnvironment.Users[ID].userry = 3;
                                                    }
                                                    #endregion

                                                    #region > ToX
                                                    while (IonEnvironment.Users[ID].userx > ToX)
                                                    {
                                                        int NewX = IonEnvironment.Users[ID].userx - 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(NewX, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",2,2/mv " + NewX + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",2,2/mv " + NewX + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].userx = NewX;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 6;
                                                        IonEnvironment.Users[ID].userry = 6;
                                                    }
                                                    #endregion

                                                    #region < ToX
                                                    while (IonEnvironment.Users[ID].userx < ToX)
                                                    {
                                                        int NewX = IonEnvironment.Users[ID].userx + 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(NewX, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",6,6/mv " + NewX + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",6,6/mv " + NewX + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].userx = NewX;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 2;
                                                        IonEnvironment.Users[ID].userry = 2;
                                                    }
                                                    #endregion

                                                    #region > ToY
                                                    while (IonEnvironment.Users[ID].usery > ToY)
                                                    {
                                                        int NewY = IonEnvironment.Users[ID].usery - 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[NewY].Substring(IonEnvironment.Users[ID].userx, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",4,4/mv " + IonEnvironment.Users[ID].userx + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",4,4/mv " + IonEnvironment.Users[ID].userx + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].usery = NewY;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 0;
                                                        IonEnvironment.Users[ID].userry = 0;
                                                    }
                                                    #endregion

                                                    #region < ToY
                                                    while (IonEnvironment.Users[ID].usery < ToY)
                                                    {
                                                        int NewY = IonEnvironment.Users[ID].usery + 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[NewY].Substring(IonEnvironment.Users[ID].userx, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",0,0/mv " + IonEnvironment.Users[ID].userx + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",0,0/mv " + IonEnvironment.Users[ID].userx + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].usery = NewY;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 4;
                                                        IonEnvironment.Users[ID].userry = 4;
                                                    }
                                                    #endregion

                                                    #region RX / RY
                                                    int RX = 0;
                                                    int RY = 0;
                                                    if (IonEnvironment.Users[ID].userrx == 0) { RX = 4; }
                                                    if (IonEnvironment.Users[ID].userrx == 1) { RX = 7; }
                                                    if (IonEnvironment.Users[ID].userrx == 2) { RX = 6; }
                                                    if (IonEnvironment.Users[ID].userrx == 3) { RX = 5; }
                                                    if (IonEnvironment.Users[ID].userrx == 5) { RX = 3; }
                                                    if (IonEnvironment.Users[ID].userrx == 6) { RX = 2; }
                                                    if (IonEnvironment.Users[ID].userrx == 7) { RX = 1; }
                                                    if (IonEnvironment.Users[ID].userrx == 4) { RX = 0; }

                                                    if (IonEnvironment.Users[ID].userry == 0) { RY = 4; }
                                                    if (IonEnvironment.Users[ID].userry == 1) { RY = 7; }
                                                    if (IonEnvironment.Users[ID].userry == 2) { RY = 6; }
                                                    if (IonEnvironment.Users[ID].userry == 3) { RY = 5; }
                                                    if (IonEnvironment.Users[ID].userry == 5) { RY = 3; }
                                                    if (IonEnvironment.Users[ID].userry == 6) { RY = 2; }
                                                    if (IonEnvironment.Users[ID].userry == 7) { RY = 1; }
                                                    if (IonEnvironment.Users[ID].userry == 4) { RY = 0; }
                                                    #endregion

                                                    if (IonEnvironment.Users[ID].dance == 1)
                                                    {
                                                        Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + "," + RX + "," + RY + "/" + OwnData + "dance/");
                                                    }
                                                    else
                                                    {
                                                        Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + "," + RX + "," + RY + "/" + OwnData + "");
                                                    }
                                                }
                                                else
                                                {
                                                    #region > ToX > ToY
                                                    while (IonEnvironment.Users[ID].userx > ToX && IonEnvironment.Users[ID].usery > ToY)
                                                    {
                                                        int NewX = IonEnvironment.Users[ID].userx - 1;
                                                        int NewY = IonEnvironment.Users[ID].usery - 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[NewY].Substring(NewX, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",7,7/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",7,7/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].userx = NewX;
                                                        IonEnvironment.Users[ID].usery = NewY;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 7;
                                                        IonEnvironment.Users[ID].userry = 7;
                                                    }
                                                    #endregion

                                                    #region > ToX < ToY
                                                    while (IonEnvironment.Users[ID].userx > ToX && IonEnvironment.Users[ID].usery < ToY)
                                                    {
                                                        int NewX = IonEnvironment.Users[ID].userx - 1;
                                                        int NewY = IonEnvironment.Users[ID].usery + 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[NewY].Substring(NewX, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",5,5/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",5,5/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].userx = NewX;
                                                        IonEnvironment.Users[ID].usery = NewY;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 5;
                                                        IonEnvironment.Users[ID].userry = 5;
                                                    }
                                                    #endregion

                                                    #region < ToX > ToY
                                                    while (IonEnvironment.Users[ID].userx < ToX && IonEnvironment.Users[ID].usery > ToY)
                                                    {
                                                        int NewX = IonEnvironment.Users[ID].userx + 1;
                                                        int NewY = IonEnvironment.Users[ID].usery - 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[NewY].Substring(NewX, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",1,1/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",1,1/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].userx = NewX;
                                                        IonEnvironment.Users[ID].usery = NewY;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 1;
                                                        IonEnvironment.Users[ID].userry = 1;
                                                    }
                                                    #endregion

                                                    #region < ToX < ToY
                                                    while (IonEnvironment.Users[ID].userx < ToX && IonEnvironment.Users[ID].usery < ToY)
                                                    {
                                                        int NewX = IonEnvironment.Users[ID].userx + 1;
                                                        int NewY = IonEnvironment.Users[ID].usery + 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[NewY].Substring(NewX, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",3,3/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",3,3/mv " + NewX + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].userx = NewX;
                                                        IonEnvironment.Users[ID].usery = NewY;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 3;
                                                        IonEnvironment.Users[ID].userry = 3;
                                                    }
                                                    #endregion

                                                    #region > ToX
                                                    while (IonEnvironment.Users[ID].userx > ToX)
                                                    {
                                                        int NewX = IonEnvironment.Users[ID].userx - 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(NewX, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",6,6/mv " + NewX + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",6,6/mv " + NewX + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].userx = NewX;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 6;
                                                        IonEnvironment.Users[ID].userry = 6;
                                                    }
                                                    #endregion

                                                    #region < ToX
                                                    while (IonEnvironment.Users[ID].userx < ToX)
                                                    {
                                                        int NewX = IonEnvironment.Users[ID].userx + 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(NewX, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",2,2/mv " + NewX + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",2,2/mv " + NewX + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].userx = NewX;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 2;
                                                        IonEnvironment.Users[ID].userry = 2;
                                                    }
                                                    #endregion

                                                    #region > ToY
                                                    while (IonEnvironment.Users[ID].usery > ToY)
                                                    {
                                                        int NewY = IonEnvironment.Users[ID].usery - 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[NewY].Substring(IonEnvironment.Users[ID].userx, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",0,0/mv " + IonEnvironment.Users[ID].userx + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",0,0/mv " + IonEnvironment.Users[ID].userx + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].usery = NewY;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 0;
                                                        IonEnvironment.Users[ID].userry = 0;
                                                    }
                                                    #endregion

                                                    #region < ToY
                                                    while (IonEnvironment.Users[ID].usery < ToY)
                                                    {
                                                        int NewY = IonEnvironment.Users[ID].usery + 1;

                                                        string RoomHeightNow = IonEnvironment.Users[ID].roomcache.Split(' ')[IonEnvironment.Users[ID].usery].Substring(IonEnvironment.Users[ID].userx, 1);
                                                        string RoomHeightNext = IonEnvironment.Users[ID].roomcache.Split(' ')[NewY].Substring(IonEnvironment.Users[ID].userx, 1);

                                                        if (IonEnvironment.Users[ID].dance == 1)
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",4,4/mv " + IonEnvironment.Users[ID].userx + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "dance/");
                                                        }
                                                        else
                                                        {
                                                            Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + RoomHeightNow + ",4,4/mv " + IonEnvironment.Users[ID].userx + "," + NewY + "," + RoomHeightNext + "/" + OwnData + "");
                                                        }
                                                        Thread.Sleep(500);

                                                        IonEnvironment.Users[ID].usery = NewY;
                                                        IonEnvironment.Users[ID].userz = RoomHeightNext;
                                                        IonEnvironment.Users[ID].userrx = 4;
                                                        IonEnvironment.Users[ID].userry = 4;
                                                    }
                                                    #endregion

                                                    if (IonEnvironment.Users[ID].dance == 1)
                                                    {
                                                        Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + "," + IonEnvironment.Users[ID].userrx + "," + IonEnvironment.Users[ID].userry + "/" + OwnData + "dance/");
                                                    }
                                                    else
                                                    {
                                                        Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + "," + IonEnvironment.Users[ID].userrx + "," + IonEnvironment.Users[ID].userry + "/" + OwnData + "");
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    catch (NullReferenceException)
                                    {

                                    }
                                    catch (IOException)
                                    {
                                        //
                                    }
                                }
                                break;
                            #endregion

                            #region GOAWAY *
                            case "GOAWAY":
                                {
                                    Send_Data_Room("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " 0,0,99,2,2/mod 0/");
                                    IonEnvironment.Users[ID].userx = 0;
                                    IonEnvironment.Users[ID].usery = 0;
                                    IonEnvironment.Users[ID].dance = 0;
                                    if (IonEnvironment.Users[ID].inroom != 0)
                                    {
                                        dbClient.ExecuteQuery("UPDATE users SET inroom = 0 WHERE name = '" + IonEnvironment.Users[ID].name + "'");
                                        dbClient.ExecuteQuery("UPDATE rooms SET inroom = inroom - 1 WHERE id = '" + IonEnvironment.Users[ID].inroom + "'");
                                        IonEnvironment.Users[ID].inroom = 0;
                                        IonEnvironment.Users[ID].owner = 0;
                                    }
                                    IonEnvironment.Users[ID].Sck.Close();
                                }
                                break;
                            #endregion

                            #region DANCE #
                            case "DANCE":
                                {
                                    Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + ",0,2,2/dance/");
                                    IonEnvironment.Users[ID].dance = 1;
                                }
                                break;
                            #endregion

                            #region STOP *
                            case "STOP":
                                {
                                    if (header[1].ToUpper() == "DANCE")
                                    {
                                        Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + ",0,2,2/");
                                        IonEnvironment.Users[ID].dance = 0;
                                    }
                                }
                                break;
                            #endregion

                            #region GETORDERINFO *
                            case "GETORDERINFO":
                                {
                                    DataTable Data = null;

                                    Data = dbClient.ReadDataTable("SELECT longname,price FROM catalogue WHERE shortname = '" + header[2] + "' LIMIT 1");

                                    if (Data != null)
                                    {
                                        foreach (DataRow Row in Data.Rows)
                                        {
                                            Send_Data("ORDERINFO" + (char)13 + (string)Row["longname"] + "/" + (int)Row["price"] + "/" + (char)13 + (int)Row["price"]);
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region GETSTRIP *
                            case "GETSTRIP":
                                {
								// "BLSI;[ID];0;S;[ID];[SPRITE];[NAME];[DESC];1;1;[COLOR]/" + Chr(13)
                                    #region new
                                    if (header[1] == "new")
                                    {
                                        DataTable Data = null;
                                        string ItemInfo = "";

                                        Data = dbClient.ReadDataTable("SELECT id,textname,itemname,itemdesc FROM useritems WHERE user = '" + IonEnvironment.Users[ID].name + "' LIMIT 0,11");

                                        if (Data != null)
                                        {
                                            foreach (DataRow Row in Data.Rows)
                                            {
                                                ItemInfo = ItemInfo + "BLSI;" + (int)Row["id"] + ";0;S;" + (int)Row["id"] + ";" + (string)Row["textname"] + ";" + (string)Row["itemname"] + ";" + (string)Row["itemdesc"] + ";1;1;0,0,0/" + (char)13;
                                            }
                                        }

                                        IonEnvironment.Users[ID].handcache = 11;
                                        Send_Data("STRIPINFO" + (char)13 + ItemInfo);
                                    }
                                    #endregion

                                    #region next
                                    if (header[1] == "next")
                                    {
                                        DataTable Data = null;
                                        string ItemInfo = "";
                                        int HandCache2 = IonEnvironment.Users[ID].handcache + 11;

                                        Data = dbClient.ReadDataTable("SELECT id,textname,itemname,itemdesc FROM useritems WHERE user = '" + IonEnvironment.Users[ID].name + "' LIMIT " + IonEnvironment.Users[ID].handcache + "," + HandCache2);


                                        if (Data != null)
                                        {
                                            foreach (DataRow Row in Data.Rows)
                                            {
                                                ItemInfo = ItemInfo + "BLSI;" + (int)Row["id"] + ";0;S;" + (int)Row["id"] + ";" + (string)Row["textname"] + ";" + (string)Row["itemname"] + ";" + (string)Row["itemdesc"] + ";1;1;0,0,0/" + (char)13;
                                            }
                                        }

                                        IonEnvironment.Users[ID].handcache = HandCache2;
                                        Send_Data("STRIPINFO" + (char)13 + ItemInfo);
                                    }
                                    #endregion
                                }
                                break;
                            #endregion

                            #region PURCHASE *
                            case "PURCHASE":
                                {
                                    DataTable Data = null;
                                    string[] DataSplit = PaketData_string.Split('/');

                                    Data = dbClient.ReadDataTable("SELECT * FROM catalogue WHERE longname = '" + DataSplit[1] + "' LIMIT 1");

                                    if (Data != null)
                                    {
                                        foreach (DataRow Row in Data.Rows)
                                        {
                                            dbClient.ExecuteQuery("INSERT INTO useritems VALUES (NULL, '" + IonEnvironment.Users[ID].name + "', '" + (string)Row["longname"] + "', '" + (string)Row["itemname"] + "', '" + (string)Row["itemdesc"] + "', '" + (string)Row["rgb"] + "', '" + (string)Row["size"] + "', '" + (string)Row["shortname"] + "')");
                                            dbClient.ExecuteQuery("UPDATE users SET credits = credits - " + DataSplit[2] + " WHERE name = '" + IonEnvironment.Users[ID].name + "'");
                                            IonEnvironment.Users[ID].credits = IonEnvironment.Users[ID].credits - (int)Row["price"];

                                            //Send_Data("SYSTEMBROADCAST" + (char)13 + "Buying successful !");
                                        }
                                    }
                                }
                                break;
                            #endregion

                            #region CREATEFLAT *
                            case "CREATEFLAT":
                                {
                                    string[] SplitData = PaketData_string.Split('/');

                                    dbClient.ExecuteQuery("INSERT INTO rooms VALUES (NULL, '" + SplitData[2] + "', '', '" + IonEnvironment.Users[ID].name + "', 'floor1', '0', '" + SplitData[3] + "', '" + SplitData[4] + "', '', '100', '100')");
                                }
                                break;
                            #endregion

                            #region MESSENGER_REQUESTBUDDY *
                            case "MESSENGER_REQUESTBUDDY":
                                {
                                    dbClient.ExecuteQuery("INSERT INTO buddy VALUES (NULL, '" + IonEnvironment.Users[ID].name + "', '" + header[1].Split('\r')[0] + "', 0)");
                                }
                                break;
                            #endregion

                            #region MESSENGER_ACCEPTBUDDY *
                            case "MESSENGER_ACCEPTBUDDY":
                                {
                                    dbClient.ExecuteQuery("UPDATE buddy SET accept = 1 WHERE from_user LIKE '" + header[1] + "' AND to_user = '" + IonEnvironment.Users[ID].name + "' AND accept = 0");

                                    DataTable Data = null;
                                    string BuddyData = "";

                                    //Data = dbClient.ReadDataTable("SELECT from_user,to_user FROM buddy WHERE to_user LIKE '" + IonEnvironment.Users[ID].name + "' AND accept = 1 OR from_user LIKE '" + IonEnvironment.Users[ID].name + "' AND accept = 1");
									Data = dbClient.ReadDataTable("SELECT from_user,to_user FROM buddy WHERE to_user = '" + IonEnvironment.Users[ID].name + "' AND accept = 1 OR from_user = '" + IonEnvironment.Users[ID].name + "' AND accept = 1");

                                    if (Data != null)
                                    {
                                        foreach (DataRow Row in Data.Rows)
                                        {
                                            string DataName = "";
                                            if ((string)Row["from_user"] != IonEnvironment.Users[ID].name)
                                            {
                                                DataName = (string)Row["from_user"];
                                            }
                                            else
                                            {
                                                DataName = (string)Row["to_user"];
                                            }

                                            BuddyData = BuddyData + (char)13 + "1 " + DataName + " UNKNOW" + (char)13;
                                        }
                                    }

                                    Send_Data("BUDDYLIST" + BuddyData);
                                }
                                break;
                            #endregion

                            #region MESSENGER_DECLINEBUDDY *
                            case "MESSENGER_DECLINEBUDDY":
                                {
                                    dbClient.ExecuteQuery("DELETE FROM buddy WHERE from_user LIKE '" + header[1] + "' AND to_user = '" + IonEnvironment.Users[ID].name + "' AND accept = 0");
                                }
                                break;
                            #endregion

                            #region MESSENGERINIT *
                            case "MESSENGERINIT":
                                {
                                    DataTable Data = null;
                                    string BuddyData = "";

                                    Data = dbClient.ReadDataTable("SELECT from_user,to_user FROM buddy WHERE to_user LIKE '" + IonEnvironment.Users[ID].name + "' AND accept = 1 OR from_user LIKE '" + IonEnvironment.Users[ID].name + "' AND accept = 1");

                                    if (Data != null)
                                    {
                                        foreach (DataRow Row in Data.Rows)
                                        {
                                            string DataName = "";
                                            if ((string)Row["from_user"] != IonEnvironment.Users[ID].name)
                                            {
                                                DataName = (string)Row["from_user"];
                                            }
                                            else
                                            {
                                                DataName = (string)Row["to_user"];
                                            }
                                            BuddyData = BuddyData + (char)13 + "1 " + DataName + " UNKNOW" + (char)13;
                                        }
                                    }

                                    Send_Data("BUDDYLIST" + BuddyData);
                                }
                                break;
                            #endregion

                            #region MESSENGER_REMOVEBUDDY *
                            case "MESSENGER_REMOVEBUDDY":
                                {														// like
                                    dbClient.ExecuteQuery("DELETE FROM buddy WHERE from_user = '" + header[1] + "' AND to_user = '" + IonEnvironment.Users[ID].name + "' AND accept = 1 LIMIT 1");
                                    dbClient.ExecuteQuery("DELETE FROM buddy WHERE to_user = '" + header[1] + "' AND from_user = '" + IonEnvironment.Users[ID].name + "' AND accept = 1 LIMIT 1");

                                    DataTable Data = null;
                                    string BuddyData = "";

                                    Data = dbClient.ReadDataTable("SELECT * FROM buddy WHERE to_user LIKE '" + IonEnvironment.Users[ID].name + "' AND accept = 1 OR from_user LIKE '" + IonEnvironment.Users[ID].name + "' AND accept = 1");

                                    if (Data != null)
                                    {
                                        foreach (DataRow Row in Data.Rows)
                                        {
                                            string DataName = "";
                                            if ((string)Row["from_user"] != IonEnvironment.Users[ID].name)
                                            {
                                                DataName = (string)Row["from_user"];
                                            }
                                            else
                                            {
                                                DataName = (string)Row["to_user"];
                                            }
                                            BuddyData = BuddyData + (char)13 + "1 " + DataName + " UNKNOW" + (char)13;
                                        }
                                    }

                                    Send_Data("BUDDYLIST" + BuddyData);
                                }
                                break;
                            #endregion

                            #region PLACESTUFFFROMSTRIP *
                            case "PLACESTUFFFROMSTRIP":
                                {
                            	// ACTIVE_OBJECTS [ID],[Sprite] [X] [Y] 1 1 [ROTATION] [HEIGHT/0.0] [COLOR][13]
								
								//# ACTIVEOBJECT_UPDATE .4692785,redhologram 6 7 1 1 0 0.0 0,0,0 /Holo-girl/You're her only hope.../SWITCHON/ON.##
								
                            	if( IonEnvironment.Users[ID].owner == 1) { // roomowner
                            		
	                                    DataTable Data = null;
	                                    DataTable RoomItemData = null;
	                                    string RoomTotalData = "";
	
	                                    
	                                    Data = dbClient.ReadDataTable("SELECT * FROM useritems WHERE id = '" + header[1] + "' LIMIT 1");
	
	                                    if (Data != null)
	                                    {
	                                        foreach (DataRow Row in Data.Rows)
	                                        {
	                                            dbClient.ExecuteQuery("INSERT INTO roomitems VALUES (NULL, '" + IonEnvironment.Users[ID].inroom + "', '" + (string)Row["textname"] + "', '" + (string)Row["itemname"] + "', '" + (string)Row["itemdesc"] + "', '" + (string)Row["rgb"] + "', '" + (string)Row["size"] + "', '" + (string)Row["shortname"] + "','0' , '" + header[2] + "', '" + header[3] + "')");
	
	                                        }
	                                    }
	
	                                    dbClient.ExecuteQuery("DELETE FROM useritems WHERE id = '" + header[1] + "'");
	                                    
	
	                                    RoomItemData = dbClient.ReadDataTable("SELECT * FROM roomitems WHERE roomid = '" + IonEnvironment.Users[ID].inroom + "' ORDER BY id DESC LIMIT 1");
	
	                                    if (RoomItemData != null)
	                                    {
	                                        foreach (DataRow RowXX in RoomItemData.Rows)
	                                        {
	                                            int zero = (int)RowXX["id"];
	                                            string zstring = "00000000000";
	                                            int j = 0;
	                                            while (j < zero.ToString().Length)
	                                            {
	                                                zstring = zstring + "0";
	                                                j++;
	                                            }
	
	                                            RoomTotalData = RoomTotalData + (char)13 + zstring + (int)RowXX["id"] + "," + (string)RowXX["textname"] + " " + (int)RowXX["x"] + " " + (int)RowXX["y"] + " " + (string)RowXX["size"] + " " + (int)RowXX["rotate"] + " 0.0 " + (string)RowXX["rgb"] + " /" + (string)RowXX["itemname"] + "/" + (string)RowXX["itemdesc"] + "/";
	                                        }
	                                    }
	
	                                    Send_Data_Room("ACTIVE_OBJECTS" + RoomTotalData);
	                                    
                            		}
                                }
                                break;
                            #endregion

                            #region MOVESTUFF *
                            case "MOVESTUFF":
                                {
                                    dbClient.ExecuteQuery("UPDATE roomitems SET x = '" + header[2] + "', y = '" + header[3] + "', rotate = '" + header[4] + "' WHERE id = '" + header[1] + "' LIMIT 1");

                                    DataTable RoomItemData = null;
                                    string RoomTotalData = "";

                                    RoomItemData = dbClient.ReadDataTable("SELECT * FROM roomitems WHERE roomid = '" + IonEnvironment.Users[ID].inroom + "' AND id = '" + header[1] + "'");

                                    if (RoomItemData != null)
                                    {
                                        foreach (DataRow RowXX in RoomItemData.Rows)
                                        {
                                            int zero = (int)RowXX["id"];
                                            string zstring = "00000000000";
                                            int j = 0;
                                            while (j < zero.ToString().Length)
                                            {
                                                zstring = zstring + "0";
                                                j++;
                                            }

                                            RoomTotalData = RoomTotalData + (char)13 + zstring + (int)RowXX["id"] + "," + (string)RowXX["textname"] + " " + (int)RowXX["x"] + " " + (int)RowXX["y"] + " " + (string)RowXX["size"] + " " + (int)RowXX["rotate"] + " 0.0 " + (string)RowXX["rgb"] + " /" + (string)RowXX["itemname"] + "/" + (string)RowXX["itemdesc"] + "/";
                                        }
                                    }

                                    Send_Data_Room("ACTIVEOBJECT_UPDATE" + RoomTotalData);
                                }
                                break;
                            #endregion

                            #region REMOVESTUFF *
                            case "REMOVESTUFF":
                                {
                                    DataTable RoomItemData = null;
                                    string RoomTotalData = "";

                                    RoomItemData = dbClient.ReadDataTable("SELECT textname,size,rotate,rgb,itemname,itemdesc FROM roomitems WHERE roomid = '" + IonEnvironment.Users[ID].inroom + "' AND id = '" + header[1] + "' LIMIT 1");

                                    if (RoomItemData != null)
                                    {
                                        foreach (DataRow RowXX in RoomItemData.Rows)
                                        {
                                            int zero = (int)RowXX["id"];
                                            string zstring = "00000000000";
                                            int j = 0;
                                            while (j < zero.ToString().Length)
                                            {
                                                zstring = zstring + "0";
                                                j++;
                                            }

                                            RoomTotalData = RoomTotalData + (char)13 + zstring + (int)RowXX["id"] + "," + (string)RowXX["textname"] + " 99 99 " + (string)RowXX["size"] + " " + (int)RowXX["rotate"] + " 0.0 " + (string)RowXX["rgb"] + " /" + (string)RowXX["itemname"] + "/" + (string)RowXX["itemdesc"] + "/";
                                        }
                                    }

                                    Send_Data_Room("ACTIVEOBJECT_UPDATE" + RoomTotalData);
                                    dbClient.ExecuteQuery("DELETE FROM roomitems WHERE id = '" + header[1] + "' LIMIT 1");
                                }
                                break;
                            #endregion

                            #region ADDSTRIPITEM *
                            case "ADDSTRIPITEM":
                                {
                                    DataTable RoomItemData = null;
                                    string RoomTotalData = "";

                                    RoomItemData = dbClient.ReadDataTable("SELECT textname,itemname,itemdesc,rgb,size,shortname FROM roomitems WHERE roomid = '" + IonEnvironment.Users[ID].inroom + "' AND id = '" + header[3] + "' LIMIT 1");

                                    if (RoomItemData != null)
                                    {
                                        foreach (DataRow RowXX in RoomItemData.Rows)
                                        {
                                            int zero = (int)RowXX["id"];
                                            string zstring = "00000000000";
                                            int j = 0;
                                            while (j < zero.ToString().Length)
                                            {
                                                zstring = zstring + "0";
                                                j++;
                                            }

                                            RoomTotalData = RoomTotalData + (char)13 + zstring + (int)RowXX["id"] + "," + (string)RowXX["textname"] + " 99 99 " + (string)RowXX["size"] + " " + (int)RowXX["rotate"] + " 0.0 " + (string)RowXX["rgb"] + " /" + (string)RowXX["itemname"] + "/" + (string)RowXX["itemdesc"] + "/";

                                            dbClient.ExecuteQuery("INSERT INTO useritems VALUES (NULL, '" + IonEnvironment.Users[ID].name + "', '" + (string)RowXX["textname"] + "', '" + (string)RowXX["itemname"] + "', '" + (string)RowXX["itemdesc"] + "', '" + (string)RowXX["rgb"] + "', '" + (string)RowXX["size"] + "', '" + (string)RowXX["shortname"] + "')");
                                            dbClient.ExecuteQuery("DELETE FROM roomitems WHERE id = '" + header[1] + "' LIMIT 1");
                                        }
                                    }

                                    Send_Data_Room("ACTIVEOBJECT_UPDATE" + RoomTotalData);
                                }
                                break;
                            #endregion

                            #region LOOKTO *
                            case "LOOKTO":
                                {
                                    int ToX = int.Parse(header[1]);
                                    int ToY = int.Parse(header[2]);

                                    string OwnData = "";
                                    if (IonEnvironment.Users[ID].owner == 1)
                                    {
                                        OwnData = "flatctrl useradmin/";
                                    }

                                    #region > ToX > ToY
                                    if (IonEnvironment.Users[ID].userx > ToX && IonEnvironment.Users[ID].usery > ToY)
                                    {
                                        if (IonEnvironment.Users[ID].dance == 1)
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",7,7/" + OwnData + "dance/");
                                        }
                                        else
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",7,7/" + OwnData + "");
                                        }

                                        IonEnvironment.Users[ID].userrx = 7;
                                        IonEnvironment.Users[ID].userry = 7;
                                    }
                                    #endregion

                                    #region > ToX < ToY
                                    else if (IonEnvironment.Users[ID].userx > ToX && IonEnvironment.Users[ID].usery < ToY)
                                    {
                                        if (IonEnvironment.Users[ID].dance == 1)
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",5,5/" + OwnData + "dance/");
                                        }
                                        else
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",5,5/" + OwnData + "");
                                        }

                                        IonEnvironment.Users[ID].userrx = 5;
                                        IonEnvironment.Users[ID].userry = 5;
                                    }
                                    #endregion

                                    #region < ToX > ToY
                                    else if (IonEnvironment.Users[ID].userx < ToX && IonEnvironment.Users[ID].usery > ToY)
                                    {
                                        if (IonEnvironment.Users[ID].dance == 1)
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",1,1/" + OwnData + "dance/");
                                        }
                                        else
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",1,1/" + OwnData + "");
                                        }

                                        IonEnvironment.Users[ID].userrx = 1;
                                        IonEnvironment.Users[ID].userry = 1;
                                    }
                                    #endregion

                                    #region < ToX < ToY
                                    else if (IonEnvironment.Users[ID].userx < ToX && IonEnvironment.Users[ID].usery < ToY)
                                    {
                                        if (IonEnvironment.Users[ID].dance == 1)
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",3,3/" + OwnData + "dance/");
                                        }
                                        else
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",3,3/" + OwnData + "");
                                        }

                                        IonEnvironment.Users[ID].userrx = 3;
                                        IonEnvironment.Users[ID].userry = 3;
                                    }
                                    #endregion

                                    #region > ToX
                                    else if (IonEnvironment.Users[ID].userx > ToX)
                                    {
                                        if (IonEnvironment.Users[ID].dance == 1)
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",6,6/" + OwnData + "dance/");
                                        }
                                        else
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",6,6/" + OwnData + "");
                                        }

                                        IonEnvironment.Users[ID].userrx = 6;
                                        IonEnvironment.Users[ID].userry = 6;
                                    }
                                    #endregion

                                    #region < ToX
                                    else if (IonEnvironment.Users[ID].userx < ToX)
                                    {
                                        if (IonEnvironment.Users[ID].dance == 1)
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",2,2/" + OwnData + "dance/");
                                        }
                                        else
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",2,2/" + OwnData + "");
                                        }

                                        IonEnvironment.Users[ID].userrx = 2;
                                        IonEnvironment.Users[ID].userry = 2;
                                    }
                                    #endregion

                                    #region > ToY
                                    else if (IonEnvironment.Users[ID].usery > ToY)
                                    {
                                        if (IonEnvironment.Users[ID].dance == 1)
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",0,0/" + OwnData + "dance/");
                                        }
                                        else
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",0,0/" + OwnData + "");
                                        }

                                        IonEnvironment.Users[ID].userrx = 0;
                                        IonEnvironment.Users[ID].userry = 0;
                                    }
                                    #endregion

                                    #region < ToY
                                    else if (IonEnvironment.Users[ID].usery < ToY)
                                    {
                                        if (IonEnvironment.Users[ID].dance == 1)
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",4,4/" + OwnData + "dance/");
                                        }
                                        else
                                        {
                                            Send_Data_Room("STATUS" + (char)13 + IonEnvironment.Users[ID].name + " " + IonEnvironment.Users[ID].userx + "," + IonEnvironment.Users[ID].usery + "," + IonEnvironment.Users[ID].userz + ",4,4/" + OwnData + "");
                                        }

                                        IonEnvironment.Users[ID].userrx = 4;
                                        IonEnvironment.Users[ID].userry = 4;
                                    }
                                    #endregion
                                }
                                break;
                            #endregion

                            #region KILLUSER *
                            case "KILLUSER":
                                {
                                    int i = 0;
                                    while (i < 150)
                                    {
                                        if (IonEnvironment.Users[i] != null)
                                        {
                                            if (IonEnvironment.Users[i].used != false)
                                            {
                                                if (IonEnvironment.Users[i].name == header[1] && IonEnvironment.Users[i].inroom == IonEnvironment.Users[ID].inroom)
                                                {
                                                    System.Text.ASCIIEncoding enc2 = new System.Text.ASCIIEncoding();
                                                    byte[] tmp2 = enc2.GetBytes(PaketHelper.Build_Send("SYSTEMBROADCAST" + (char)13 + "You are kicked out of the room!").ToCharArray());
                                                    IonEnvironment.Users[i].Sck.GetStream().Write(tmp2, 0, tmp2.Length);

                                                    IonEnvironment.Users[i].Sck.Close();
                                                }
                                            }
                                        }
                                        i++;
                                    }
                                }
                                break;
                            #endregion

                            #region ASSIGNRIGHTS *
                            case "ASSIGNRIGHTS":
                                {
                                    dbClient.ExecuteQuery("INSERT INTO rights VALUES (NULL, '" + IonEnvironment.Users[ID].inroom + "', '" + header[1] + "')");

                                    int i = 0;
                                    while (i < 150)
                                    {
                                        if (IonEnvironment.Users[i] != null)
                                        {
                                            if (IonEnvironment.Users[i].used != false)
                                            {
                                                if (IonEnvironment.Users[i].name == header[1] && IonEnvironment.Users[i].inroom == IonEnvironment.Users[ID].inroom)
                                                {
                                                    System.Text.ASCIIEncoding enc2 = new System.Text.ASCIIEncoding();
                                                    byte[] tmp2 = enc2.GetBytes(PaketHelper.Build_Send("YOUARECONTROLLER").ToCharArray());
                                                    IonEnvironment.Users[i].Sck.GetStream().Write(tmp2, 0, tmp2.Length);
                                                    IonEnvironment.Users[i].rights = 1;
                                                }
                                            }
                                        }
                                        i++;
                                    }
                                }
                                break;
                            #endregion

                            #region REMOVERIGHTS *
                            case "REMOVERIGHTS":
                                {
                                    dbClient.ExecuteQuery("DELETE FROM rights WHERE room = '" + IonEnvironment.Users[ID].inroom + "' AND user = '" + header[1] + "'");
                                }
                                break;
                            #endregion

                            #region UPDATEFLAT *
                            case "UPDATEFLAT":
                                {
                                    string[] SplitData = PaketData_string.Split('/');
                                    dbClient.ExecuteQuery("UPDATE rooms SET `name` = '" + SplitData[2] + "', `door` = '" + SplitData[3] + "' WHERE id = '" + SplitData[1] + "' LIMIT 1");
                                }
                                break;
                            #endregion

                            #region SETFLATINFO *
                            case "SETFLATINFO":
                                {
                                    string[] SplitData = PaketData_string.Split('/');
                                    string[] SplitData2 = SplitData[2].Split((char)13);
                                    dbClient.ExecuteQuery("UPDATE rooms SET `desc` = '" + SplitData2[0].Split('=')[1] + "', `pass` = '" + SplitData2[1].Split('=')[1] + "' WHERE id = '" + SplitData[1] + "'");
                                }
                                break;
                            #endregion
                        }

                        if (IonEnvironment.Users[ID].name != null)
                        {
                            DataTable Data01 = null;

                            Data01 = dbClient.ReadDataTable("SELECT * FROM buddy WHERE to_user LIKE '" + IonEnvironment.Users[ID].name + "' AND accept = 0");

                            if (Data01 != null)
                            {
                                foreach (DataRow Row01 in Data01.Rows)
                                {
                                    Send_Data("BUDDYADDREQUESTS" + (char)13 + "/" + (string)Row01["from_user"]);
                                }
                            }
                        }
                        Thread.Sleep(20);
                    }
                    Thread.Sleep(20);
                }
                
                IonEnvironment.GetLog().WriteError("Test, Close: " + ID);

                Send_Data_All("STATUS " + (char)13 + IonEnvironment.Users[ID].name + " 0,0,99,2,2/mod 0/");
                if (IonEnvironment.Users[ID].inroom != 0 && IonEnvironment.Users[ID].name != null)
                {
                    dbClient.ExecuteQuery("UPDATE users SET inroom = 0 WHERE name = '" + IonEnvironment.Users[ID].name + "'");
                    dbClient.ExecuteQuery("UPDATE rooms SET inroom = inroom - 1 WHERE id = '" + IonEnvironment.Users[ID].inroom + "'");
                    IonEnvironment.Users[ID].inroom = 0;
                    IonEnvironment.Users[ID].owner = 0;
                }
                IonEnvironment.Users[ID].used = false;
            }
            catch (Exception ex)
            {
                IonEnvironment.GetLog().WriteError("TcpSck: " + ex.Message);
            }
        }
        
        private void Send_Data(string data)
        {
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            byte[] tmp = enc.GetBytes(PaketHelper.Build_Send(data).ToCharArray());
            IonEnvironment.Users[ID].Sck.GetStream().Write(tmp, 0, tmp.Length);

            IonEnvironment.GetLog().WriteDebug("<-- " + data.Replace("\r", " {13} "));
        }


		private void Send_Data_All(string data)
		{
			int i = 0;
			while (i < maxcon) //150 // OUT OF BOUNDS !!!!!!
			{
				if (IonEnvironment.Users[i] != null)
				{
					if (IonEnvironment.Users[i].used != false)
					{
						try
						{
							System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
							byte[] tmp = enc.GetBytes(PaketHelper.Build_Send(data).ToCharArray());
							IonEnvironment.Users[i].Sck.GetStream().Write(tmp, 0, tmp.Length);
						}
						catch (Exception)
						{
							// ** //
						}
					}
				}
				i++;
			}

			IonEnvironment.GetLog().WriteDebug("<-- " + data.Replace("\r", " {13} "));
		}

		private void Send_Data_Room(string data)
		{
			int i = 0;
			while (i < maxcon) //150 // OUT OF BOUNDS !!!!!!
			{
				if (IonEnvironment.Users[i] != null)
				{
					if (IonEnvironment.Users[i].used != false)
					{
						if (IonEnvironment.Users[i].inroom == IonEnvironment.Users[ID].inroom)
						{
							try
							{
								System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
								byte[] tmp = enc.GetBytes(PaketHelper.Build_Send(data).ToCharArray());
								IonEnvironment.Users[i].Sck.GetStream().Write(tmp, 0, tmp.Length);
							}
							catch
							{
								// ** //

							}

						}
					}
				}
				i++;
			}

			IonEnvironment.GetLog().WriteDebug("<-- " + data.Replace("\r", " {13} "));
		}


		

    }
}