/*
		/// <summary>
		/// Accept incoming connection for NPC-Control
		/// </summary>
		/// <param name="ar"></param>
		protected void NCControl_Accept(IAsyncResult ar)
		{
			// Accept Socket
			NCConnection Client = new NCConnection(this, (Socket)NCListen.EndAcceptSocket(ar));
			Client.ReceiveData();
			NCList.Add(Client);

			// Listen for incoming connections
			NCListen.BeginAcceptSocket(cNCAccept, NCListen);
		}
		*/
		/// <summary>
		/// Run NPC Server
		/// </summary>
		public void RunServer()
		{
			// Run Timeouts
			/*
			foreach (KeyValuePair<String, ServerWeapon> wep in WeaponList)
			{
				ScriptObj obj = (ScriptObj)wep.Value.ScriptObject;
				if (obj != null)
					obj.RunEvents();
			}
			
			// Level-Npcs
			lock (TimerLock)
			{
				foreach (KeyValuePair<String, GraalLevel> lvl in LevelList)
				{
					foreach (KeyValuePair<int, GraalLevelNPC> npc in lvl.Value.NpcList)
					{
						ScriptObj obj = (ScriptObj)npc.Value.ScriptObject;
						if (obj != null)
							obj.RunEvents();
					}
				}
			}
			
			while (Compiler.RunList.Count > 0)
			{
				IRefObject obj;
				lock (Compiler.RunList)
					obj = Compiler.RunList.Dequeue();
				obj.Call("onCreated", null);
			}
			*/
			this.form = Abstraction.GetInstance();

			this.form.Activate();
			this.form.WriteText("--- OpenGraal Instant Messenger / Remote Control ---");
			this.serverWindow = ServerWindow.GetInstance();
			// Default PM
			this.NCMsg = CString.tokenize("I am the Framework for\nthis game server. Almost\nall npc actions are controled\nby me.");

			// Create Compiler
			//Compiler = new GameCompiler(this);

			// Create Player Manager
			//PlayerManager = new GraalPlayerList();
			
			// Connect to GServer
			GSConn = new Connections.GraalServer();
			NCConn = new NCConnection();

			GSConn.RunServer();
			
			if (this.serverWindow.nickname != null && this.serverWindow.nickname != "")
				this.nickname = this.serverWindow.nickname;
			
			if (this.serverWindow.username != null && this.serverWindow.username != "")
				this.username = this.serverWindow.username;

			if (this.serverWindow.password != null && this.serverWindow.password != "")
				this.password = this.serverWindow.password;

			if (this.serverWindow.gserver != null && this.serverWindow.gserver != "")
				server = this.serverWindow.gserver;

			if (this.serverWindow.gport != null && this.serverWindow.gport != 0)
				this.port = this.serverWindow.gport;

			//GSConn.Disconnect();
			if (!GSConn.Connected)
			{
				this.form.WriteText("Not connected, connecting...");
				this.form.WriteText("Address:\t" + this.server);
				this.form.WriteText("Port:\t\t" + this.port.ToString());
				GSConn.Connect(this.server, this.port);

			}
			else
				this.form.WriteText("Already connected!");

			if (GSConn.Connected)
			{
				GSConn.SendLogin(this.username, this.password, this.nickname);
			}
		}
		/// <summary>
		/// Run NPC Server
		/// </summary>
		public void CloseServers()
		{
			this.GSConn.Disconnect();
			this.NCConn.Disconnect();
			this.GSConn = null;
			this.NCConn = null;
			//this.NCListen.Stop();
		}