Index: binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/TelnetConnection.cs
===================================================================
--- binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/TelnetConnection.cs	(revision 174)
+++ binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/TelnetConnection.cs	(revision 182)
@@ -1,6 +1,9 @@
 using System;
+using System.Collections.Generic;
+using System.IO;
 using System.Net;
 using System.Net.Sockets;
 using System.Text;
+using System.Threading;
 
 namespace AllocsFixes.NetConnections.Servers.Telnet
@@ -8,10 +11,11 @@
 	public class TelnetConnection : IConnection
 	{
+		BlockingQueue<string> toClientQueue = new BlockingQueue<string> ();
+		private Thread receiveThread = null;
+		private Thread sendThread = null;
 		private bool authenticated = false;
 		private bool authEnabled;
 		private TcpClient client;
 		private NetworkStream stream;
-		private int lineBufferLength = 0;
-		private byte[] lineBuffer = new byte[200];
 		private EndPoint endpoint;
 
@@ -22,34 +26,120 @@
 			this.stream = client.GetStream ();
 			this.endpoint = client.Client.RemoteEndPoint;
+
+			Log.Out ("Telnet connection from: " + endpoint);
+
+			receiveThread = ThreadMaster.Create ("TelnetClientReceive", new ThreadStart (ReceiveThread));
+			receiveThread.Start ();
+			sendThread = ThreadMaster.Create ("TelnetClientSend", new ThreadStart (SendThread));
+			sendThread.Start ();
+
+			if (authEnabled) {
+				WriteLine ("Please enter password:");
+			} else {
+				LoginMessage ();
+			}
 		}
 
-		public EndPoint GetEndPoint ()
+		private void LoginMessage ()
 		{
-			return endpoint;
+			WriteLine ("*** Connected with 7DTD server.");
+			WriteLine ("*** Dedicated server only build");
+			WriteLine ("*** Allocs server fixes loaded");
+			WriteLine (string.Empty);
+			WriteLine ("Server IP:   " + 
+				((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any")
+			);
+			WriteLine ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort));
+			WriteLine ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount));
+			WriteLine ("Game mode:   " + GamePrefs.GetString (EnumGamePrefs.GameMode));
+			WriteLine ("World:       " + GamePrefs.GetString (EnumGamePrefs.GameWorld));
+			WriteLine ("Game name:   " + GamePrefs.GetString (EnumGamePrefs.GameName));
+			WriteLine ("Difficulty:  " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty));
+			WriteLine (string.Empty);
+			WriteLine ("Press 'help' to get a list of all commands. Press 'exit' to end session.");
+			WriteLine (string.Empty);
 		}
 
-		private void WriteByte (byte v)
+		private void ReceiveThread ()
 		{
-			if (!IsClosed () && stream.CanWrite) {
-				stream.WriteByte (v);
+			try {
+				StreamReader reader = new StreamReader (stream);
+				try {
+					while (!IsClosed()) {
+						string line = reader.ReadLine ();
+						if (line != null) {
+							line = line.Trim ();
+
+							if (!IsAuthenticated ()) {
+								if (line.Equals (GamePrefs.GetString (EnumGamePrefs.TelnetPassword))) {
+									authenticated = true;
+									WriteLine ("Logon successful.");
+									WriteLine (string.Empty);
+									WriteLine (string.Empty);
+									WriteLine (string.Empty);
+									LoginMessage ();
+								} else {
+									WriteLine ("Password incorrect, please enter password:");
+								}
+							} else {
+								if (line.ToLower ().Equals ("exit")) {
+									Log.Out ("Telnet connection closed by client: " + endpoint);
+									Close ();
+									break;
+								}
+								Log.Out ("Telnet executed \"" + line + "\" from: " + endpoint);
+								ConsoleOutputSeparator.QueueNetCommand (line, this);
+							}
+						}
+					}
+				} catch (IOException) {
+				} catch (ThreadInterruptedException) {
+				}
+				ThreadMaster.Remove (Thread.CurrentThread.Name);
+			} catch (Exception ex) {
+				Log.Out ("Error in TelnetClientReceive: " + ex);
 			}
+
+		}
+
+		private void SendThread ()
+		{
+			try {
+				while (!IsClosed() && stream.CanWrite) {
+					try {
+						string line = toClientQueue.Dequeue ();
+						if (!IsClosed () && stream.CanWrite) {
+							byte[] utfData = Encoding.UTF8.GetBytes (line);
+							stream.Write (utfData, 0, utfData.Length);
+							stream.WriteByte (13);
+							stream.WriteByte (10);
+						}
+					} catch (IOException) {
+					} catch (ThreadInterruptedException) {
+					} catch (Exception e) {
+						Log.Out ("Error writing to client: " + e);
+					}
+				}
+
+				ThreadMaster.Remove (Thread.CurrentThread.Name);
+			} catch (Exception ex) {
+				Log.Out ("Error in TelnetClientSend: " + ex);
+			}
+
 		}
 
 		public void WriteLine (string s)
 		{
-			try {
-				if (!IsClosed () && stream.CanWrite) {
-					byte[] utfData = Encoding.UTF8.GetBytes (s);
-					stream.Write (utfData, 0, utfData.Length);
-					WriteByte (13);
-					WriteByte (10);
-				}
-			} catch (Exception e) {
-				Log.Out ("Error writing to client: " + e);
-			}
+			toClientQueue.Enqueue (s);
 		}
 
 		public void Close ()
 		{
+			if (receiveThread != null) {
+				receiveThread.Interrupt ();
+			}
+			if (sendThread != null) {
+				sendThread.Interrupt ();
+			}
 			if (client != null)
 				client.Close ();
@@ -61,4 +151,5 @@
 			if (client != null && !client.Connected) {
 				Log.Out ("Telnet connection interrupted: " + endpoint);
+				Close ();
 			}
 			return (client == null) || (!client.Connected);
@@ -70,32 +161,4 @@
 		}
 
-		public void SetAuthenticated ()
-		{
-			authenticated = true;
-		}
-
-		public bool Read ()
-		{
-			while (!IsClosed() && stream.CanRead && stream.DataAvailable) {
-				int b = stream.ReadByte ();
-				if (b == '\r' || b == '\n') {
-					if (lineBufferLength > 0) {
-						return true;
-					}
-				} else if (b >= 0) {
-					lineBuffer [lineBufferLength] = (byte)b;
-					lineBufferLength++;
-				}
-			}
-			return false;
-		}
-
-		public string GetLine ()
-		{
-			string res = Encoding.UTF8.GetString (lineBuffer, 0, lineBufferLength);
-			lineBufferLength = 0;
-			return res;
-		}
-
 	}
 }
