public void SendMessageToServer(string argCommandString)
{
// create a Packet object from the passed data; this packet can be any object type because we use serialization!
//Dim mPacket As New Dictionary(Of String, String)
//mPacket.Add("CMD", argCommandString)
//mPacket.Add("MSG", argMessageString)
string mPacket = argCommandString;
// serialize the Packet into a stream of bytes which is suitable for sending with the Socket
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter mSerializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream mSerializerStream = new System.IO.MemoryStream();
mSerializer.Serialize(mSerializerStream, mPacket);
// get the serialized Packet bytes
byte[] mPacketBytes = mSerializerStream.GetBuffer();
// convert the size into a byte array
byte[] mSizeBytes = BitConverter.GetBytes(mPacketBytes.Length + 4);
// create the async state object which we can pass between async methods
SocketGlobals.AsyncSendState mState = new SocketGlobals.AsyncSendState(cClientSocket);
// resize the BytesToSend array to fit both the mSizeBytes and the mPacketBytes
// ERROR: Not supported in C#: ReDimStatement
Array.Resize(ref mState.BytesToSend, mPacketBytes.Length + mSizeBytes.Length);
// copy the mSizeBytes and mPacketBytes to the BytesToSend array
System.Buffer.BlockCopy(mSizeBytes, 0, mState.BytesToSend, 0, mSizeBytes.Length);
System.Buffer.BlockCopy(mPacketBytes, 0, mState.BytesToSend, mSizeBytes.Length, mPacketBytes.Length);
cClientSocket.BeginSend(mState.BytesToSend, mState.NextOffset(), mState.NextLength(), SocketFlags.None, new AsyncCallback(MessagePartSent), mState);
}