Index: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Net/BMXNetSessionSocketConnection.cs
===================================================================
--- BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Net/BMXNetSessionSocketConnection.cs	(revision 1146)
+++ BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Net/BMXNetSessionSocketConnection.cs	(revision 1180)
@@ -24,4 +24,5 @@
         public static int DefaultReceiveTimeout = 40000;
         
+        
         private int _sendTimeout = 0;
 
@@ -63,4 +64,11 @@
         }
 
+        public override Encoding ConnectionEncoding // Default Encoding determined by Windows; typically a Windows Code Page
+        {
+            get;
+            set;
+        }
+            
+
 		public BMXNetSessionSocketConnection(BMXNetBroker aBroker):base(aBroker)
 		{
@@ -70,4 +78,6 @@
 			m_sWISH = "";
 			m_cHDR = ADEBHDR(m_sWKID,m_sWINH,m_sPRCH,m_sWISH);
+
+            ConnectionEncoding = Encoding.Default;
 
 		}
@@ -218,7 +228,4 @@
         }
 
-
-
-
         bool m_bLogging = false;
 
@@ -239,7 +246,18 @@
             {
                 String cMSG = ADEBLDMsg(m_cHDR, "BMX CONNECT STATUS", "");
-
+#if DEBUG
+                _watch = new Stopwatch();
+                _watch.Start();
+#endif
                 this.SendString(this.Socket, cMSG);
                 String strReceive = ReceiveString(this.Socket);
+#if DEBUG
+                _watch.Stop();
+
+                Debug.WriteLine("Time: " + _watch.ElapsedMilliseconds + " ms");
+                Debug.WriteLine("---");
+                _watch = null;
+#endif
+
                 String port = BMXNetBroker.Piece(strReceive, "|", 1);
                 String message = BMXNetBroker.Piece(strReceive, "|", 2);
@@ -275,16 +293,18 @@
         }
 
-     
+        Stopwatch _watch = new Stopwatch();
+
         protected override String SendReceiveString(string cSendString, string cMult)
         {
 #if DEBUG
-            Stopwatch _watch = new Stopwatch();
+            _watch = new Stopwatch();
             _watch.Start();
-            Debug.WriteLine("Sending (T:" + Thread.CurrentThread.ManagedThreadId + "): " + cSendString);
 #endif
             this.SendString(this.m_pCommSocket, cSendString, cMult);
+            Debug.WriteLine("Time After Send: " + _watch.ElapsedMilliseconds + " ms");
             string _received = this.ReceiveString(this.m_pCommSocket);
 #if DEBUG
-            Debug.WriteLine("Received(T:" + Thread.CurrentThread.ManagedThreadId + "): " + _received.Replace((char) 30, (char) 10));
+            _watch.Stop();
+
             Debug.WriteLine("Time: " + _watch.ElapsedMilliseconds + " ms");
             Debug.WriteLine("---");
@@ -303,9 +323,12 @@
         protected virtual void SendString(TcpClient tcpClient, string cSendString, string cMult)
 		{
+#if DEBUG
+            Debug.WriteLine("Sending (T:" + Thread.CurrentThread.ManagedThreadId + "): " + cSendString);
+#endif
             String encodedString = this.EncodeSendString(cSendString, cMult);
 		
 			NetworkStream ns = tcpClient.GetStream();
             ns.WriteTimeout = this.SendTimeout;
-			byte[] sendBytes = Encoding.ASCII.GetBytes(encodedString);
+			byte[] sendBytes = ConnectionEncoding.GetBytes(encodedString);
 			ns.Write(sendBytes,0,sendBytes.Length);
             if (this.m_bLogging == true)
@@ -319,5 +342,69 @@
 		private string ReceiveString(TcpClient tcpClient)
 		{
-			NetworkStream ns = tcpClient.GetStream();
+#if DEBUG
+            Debug.WriteLine("Time At Start of Receive: " + _watch.ElapsedMilliseconds + " ms");
+#endif
+
+            NetworkStream ns = tcpClient.GetStream();
+            ns.ReadTimeout = this.ReceiveTimeout; //Timeout; throw exception automatically if we time out.
+
+            MemoryStream mStream = new MemoryStream(1500); // Auto expanding memory storage for what we receive
+            int numberOfBytesRead = 0;  
+            bool bFinished = false;  // Have we found the End of Transmission (ASCII 4) yet?
+
+            // Main Read Loop
+            while (!bFinished) //while we are not done
+            {
+                byte[] bReadBuffer = new byte[1500];        //buffer size = MTU. Message MUST be smaller.
+                numberOfBytesRead = ns.Read(bReadBuffer, 0, bReadBuffer.Length);    //read data
+                bFinished = FindChar(bReadBuffer, (char)4) > -1;                    //look for the EOT (ASCII 4) character
+                if (!bFinished) mStream.Write(bReadBuffer, 0, numberOfBytesRead);  //if we are not done, put the whole stream in
+                else mStream.Write(bReadBuffer, 0, numberOfBytesRead - 1);           //otherwise, number of bytes minus the $C(4) at the end
+            }
+            
+            //Read Security S-Packet
+            mStream.Seek(0, SeekOrigin.Begin);
+            int secLength = mStream.ReadByte();
+            byte[] secBytes = new byte[secLength];
+            if (secLength > 0)
+            {
+                mStream.Read(secBytes, 0, secLength);
+            }
+
+            //Read Error S-Packet
+            mStream.Seek(secLength + 1, SeekOrigin.Begin);
+            int errLength = mStream.ReadByte();
+            byte[] errBytes = new byte[errLength];
+            if (errLength > 0)
+            {
+                mStream.Read(errBytes, 0, errLength);
+            }
+
+            //If either one of these exists, then we have an error on the Mumps Database
+            if (secLength > 0 || errLength > 0)
+            {
+                string errString = ConnectionEncoding.GetString(secBytes);
+                string appString = ConnectionEncoding.GetString(errBytes);
+                throw new BMXNetException("Mumps Database Security/Error: " + errString + " | " + appString);
+            }
+
+            //No Errors. Decode the entire message starting from after the S packets above
+            string sReadBuffer = ConnectionEncoding.GetString(mStream.ToArray(), secLength + errLength + 2, mStream.ToArray().Length - (secLength + errLength + 2)); //decode
+
+            String decodedReceiveString = this.DecodeReceiveString(sReadBuffer);
+
+            if (this.m_bLogging)
+            {
+                Log("Received: " + decodedReceiveString, this.m_LogWriter);
+            }
+#if DEBUG
+            Debug.WriteLine("Time At End of Receive: " + _watch.ElapsedMilliseconds + " ms");
+            Debug.WriteLine("Received(T:" + Thread.CurrentThread.ManagedThreadId + "): " + decodedReceiveString.Replace((char)30, (char)10));
+#endif
+            return decodedReceiveString;
+            
+
+            /* OLD CODE
+            NetworkStream ns = tcpClient.GetStream();
             ns.ReadTimeout = this.ReceiveTimeout;
 
@@ -409,6 +496,9 @@
                 Log("Received: " + decodedReceiveString, this.m_LogWriter);
             }
+#if DEBUG
+            Debug.WriteLine("Time At End of Receive: " + _watch.ElapsedMilliseconds + " ms");
+#endif
             return decodedReceiveString;
-			
+			*/
 		}
 
@@ -422,7 +512,18 @@
 			//Build AV Call
 			cMSG = ADEBLDMsg(m_cHDR, "BMX AV CODE", winIdentity.Name);
+#if DEBUG
+            _watch = new Stopwatch();
+            _watch.Start();
+#endif
             SendString(this.Socket, cMSG);
-
             strReceive = ReceiveString(this.Socket);
+#if DEBUG
+            _watch.Stop();
+
+            Debug.WriteLine("Time: " + _watch.ElapsedMilliseconds + " ms");
+            Debug.WriteLine("---");
+            _watch = null;
+#endif
+
 			sTest = strReceive.Substring(0,3);
 
@@ -453,7 +554,17 @@
 		
 			cMSG = ADEBLDMsg(m_cHDR, "XUS AV CODE", encryptedAccessVerifyCode);
+#if DEBUG
+            _watch = new Stopwatch();
+            _watch.Start();
+#endif      
             SendString(this.Socket, cMSG);
-
             strReceive = ReceiveString(this.Socket);
+#if DEBUG
+            _watch.Stop();
+
+            Debug.WriteLine("Time: " + _watch.ElapsedMilliseconds + " ms");
+            Debug.WriteLine("---");
+            _watch = null;
+#endif
 
             if (strReceive.StartsWith("M ERROR="))
@@ -519,7 +630,15 @@
 					throw new BMXNetException("BMXNetBroker is not connected to RPMS");
 				}
-
+#if DEBUG
+                _watch = new Stopwatch();
+                _watch.Start();
+#endif      
                 this.SendString(this.Socket, ADEBLDMsg(m_cHDR, "BMXGetFac", aDuz));
                 return this.ReceiveString(this.Socket);
+#if DEBUG
+                _watch = new Stopwatch();
+                _watch.Start();
+#endif
+
 			}
 			catch (BMXNetException bmxEx)
