I have an object that is receiving data using Asynchronous Socket I/O and then routing the bytes up via an event to another object where that data is to be parsed. I am using a global StringBuilder object to hold message fragments and then parsing the string for the requisite data. However, I am getting inconsistent results. I don't know if it is a synchronization issue or what but I am getting a lot of duplicates in my results. Here is the code:

Event Handler containing the message bytes:
Code:
private void WinsockObjRef_OnDataArrival(object theEventRaiserObj, EventArgs_Winsock_DataArrival theEventArgsObj)
{
            string strResponse = null;

            if (theEventArgsObj.MessageBuffer[0] == 0)
                strResponse = TWGlobals.ConvertByteArrayToString(theEventArgsObj.MessageBuffer, TWGlobals.HeaderBufferSize, theEventArgsObj.MessageBuffer.Length - TWGlobals.HeaderBufferSize);
            else
                strResponse = TWGlobals.ConvertByteArrayToString(theEventArgsObj.MessageBuffer, 0, theEventArgsObj.MessageBuffer.Length);

            Parse(strResponse, theEventArgsObj);
}
Here is the parsing code:
Code:
       private void Parse(string theMessageStr, EventArgs_Winsock_DataArrival theEventArgsObj)
        {
            StringBuilder sbMessageBuilder = new StringBuilder();
            int intStartPos = 0;
            int intEndPos = 0;

            if (MessageBuilder.Length > 0)
            {
                sbMessageBuilder.Append(MessageBuilder.ToString().Trim(TWGlobals.InvalidMessageContent));
                MessageBuilder.ToString().Remove(0, MessageBuilder.Length);
            }

            sbMessageBuilder.Append(theMessageStr.Trim(TWGlobals.InvalidMessageContent));

            while (true)
            {
                intStartPos = sbMessageBuilder.ToString().IndexOf(StartHeader, intEndPos);

                if (intStartPos == TWGlobals.InvalidStringSearchHandle)
                    break;

                intEndPos = sbMessageBuilder.ToString().IndexOf(EndHeader, intStartPos);

                if (intEndPos == TWGlobals.InvalidStringSearchHandle)
                {
                    MessageBuilder.Append(sbMessageBuilder.ToString().Substring(intStartPos, sbMessageBuilder.Length - intStartPos));
                    break;
                }

                intEndPos += EndHeader.Length;

                string strMessage = sbMessageBuilder.ToString().Substring(intStartPos, (intEndPos - intStartPos));

                intStartPos = intEndPos;

                EventEntry_ResponseReceived(new EventArgs_TWmdsMessageRouter_ResponseReceived(strMessage, strMessage.Length, theEventArgsObj.TWConnectionObjRef, theEventArgsObj.UserEventMessage));
            }

        }