Index: /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/BMXNetSessionConnection.cs
===================================================================
--- /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/BMXNetSessionConnection.cs	(revision 1179)
+++ /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/BMXNetSessionConnection.cs	(revision 1180)
@@ -441,4 +441,5 @@
         public abstract int ReceiveTimeout { get; set; }
         public abstract int SendTimeout { get; set; }
+        public abstract Encoding ConnectionEncoding { get; set; }
 
 
@@ -469,5 +470,7 @@
                 // The writer lock request timed out.
                 //TODO: Remark: to writer lock for Transmit RPC
-                Debug.Write("TransmitRPC writer lock request timed out.\n");
+                //SMH: Wrong error message. We do have an exception, but nothing to do with a lock.
+                //Debug.Write("TransmitRPC writer lock request timed out.\n");
+                Debug.Write("Exception: " + exception.Message);
                 throw exception;
             }
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 1179)
+++ /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)
Index: /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/RemoteSession.cs
===================================================================
--- /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/RemoteSession.cs	(revision 1179)
+++ /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/RemoteSession.cs	(revision 1180)
@@ -47,4 +47,6 @@
         /// </example>
         String AppContext { get; set; }
+
+        Encoding ConnectionEncoding { get; set; }
 
         #endregion
@@ -680,5 +682,14 @@
         /// <param name="aString">The command to be examined</param>
         /// <returns>True if it complied, otherwise false</returns>
-        bool IsBmxAdoCommand(String aString); 
+        bool IsBmxAdoCommand(String aString);
+
+        /// <summary>
+        /// Locks a Name on the Mumps Database
+        /// </summary>
+        /// <param name="g/lvnToLock">Global or Local Name to Lock</param>
+        /// <param name="addOrSubstract">+ or - to increment or decrement the lock</param>
+        /// <returns>true for success; false for failure</returns>
+        bool Lock(string lvnToLock, string addOrSubstract);
+
         #endregion
 
Index: /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Sevices/BMXNetRemoteSession.cs
===================================================================
--- /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Sevices/BMXNetRemoteSession.cs	(revision 1179)
+++ /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Sevices/BMXNetRemoteSession.cs	(revision 1180)
@@ -1,4 +1,5 @@
 using System;
 using System.Data;
+using System.Text;
 using System.Windows.Forms;
 using IndianHealthService.BMXNet.Model;
@@ -76,4 +77,16 @@
         }
 
+        public Encoding ConnectionEncoding
+        {
+            get
+            {
+                return SessionConnection.ConnectionEncoding;
+            }
+            set
+            {
+                SessionConnection.ConnectionEncoding = value;
+            }
+        }
+
         public bool IsEventPollingEnabled
         {
@@ -509,7 +522,8 @@
         }
 
+        //smh: THIS IS WRONG. If you pass the data set, why make a new dataset...
         public DataTable TableFromSQL(string sql, DataSet aDataSet, string aTableName)
         {
-            return this.TableFromSQL(sql, new DataSet(), this.DefaultTableName, this.AppContext);
+            return this.TableFromSQL(sql, aDataSet, aTableName, this.AppContext);
         }
 
@@ -1070,4 +1084,9 @@
             }                    
         }
+
+        public bool Lock(string lvnToLock, string addOrSubstract)
+        {
+            return SessionConnection.Lock(lvnToLock, addOrSubstract);
+        }
     }
 }
Index: /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Sevices/BMXNetSessionConnectionOverAnotherSessionConnection.cs
===================================================================
--- /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Sevices/BMXNetSessionConnectionOverAnotherSessionConnection.cs	(revision 1179)
+++ /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/Sevices/BMXNetSessionConnectionOverAnotherSessionConnection.cs	(revision 1180)
@@ -35,4 +35,10 @@
         }
 
+        public override Encoding ConnectionEncoding
+        {
+            get;
+            set;
+        }
+
         public override void Close()
         {
Index: /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/bmxnet.xml
===================================================================
--- /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/bmxnet.xml	(revision 1179)
+++ /BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet/bmxnet.xml	(revision 1180)
@@ -1051,4 +1051,12 @@
             <param name="aString">The command to be examined</param>
             <returns>True if it complied, otherwise false</returns>
+        </member>
+        <member name="M:IndianHealthService.BMXNet.RemoteSession.Lock(System.String,System.String)">
+            <summary>
+            Locks a Name on the Mumps Database
+            </summary>
+            <param name="g/lvnToLock">Global or Local Name to Lock</param>
+            <param name="addOrSubstract">+ or - to increment or decrement the lock</param>
+            <returns>true for success; false for failure</returns>
         </member>
         <member name="M:IndianHealthService.BMXNet.RemoteSession.Close">
