public void FindPacket(byte[] buffer) { int currentBufferByte = 0; while (currentBufferByte < buffer.Length) { if (ByteStuffing.FlagSymbol == buffer[currentBufferByte]) { if (escapeFlag) { TryPacketBuild(buffer[currentBufferByte]); } else { byte[] recvPacket = new byte[packetTail]; Array.Copy(packetBuffer, recvPacket, packetTail); packetHandler.ProcessPacket(recvPacket); packetTail = 0; } } else if (ByteStuffing.EscSymbol == buffer[currentBufferByte]) { if (escapeFlag) { TryPacketBuild(buffer[currentBufferByte]); } else { escapeFlag = true; } } else { TryPacketBuild(buffer[currentBufferByte]); } currentBufferByte++; } }