if-elseif chain. Is there a better pattern? I'm working on a TCP Server using .NET and the following code is my first attempt to process the received data (from the socket) to create a packet to deliver. Basically, the packet has a header (7 bytes) where the first 3 bytes are always 0x12 0x34 0x89 and the next 7 bytes are the message length. For example:  

0x12 0x34 0x89 0x00 0x00 0x00 0x02 0x20 0x20

The packet then is a 2 length packet and the sent data is 0x20 0x20.

To process this I am using an if-elseif chain, which I don't like. Is there a better pattern to take away that if-elseif chain?  

    public void ProcessIncomingData(byte[] buffer, int length)
    {
        for (int i = 0; i &lt; length; i++)
        {
            ProcessByte(buffer[i]);
        }
    }

    private void ProcessByte(byte b)
    {
        if (_status == PacketStatus.Empty &amp;&amp; b == FirstHeaderByte)
        {
            _status = PacketStatus.FirstHeaderReceived;
        }
        else if (_status == PacketStatus.FirstHeaderReceived &amp;&amp; b == SecondHeaderByte)
        {
            _status = PacketStatus.SecondHeaderReceived;
        }
        else if (_status == PacketStatus.SecondHeaderReceived &amp;&amp; b == ThridHeaderByte)
        {
            _status = PacketStatus.ThirdHeaderReceived;
        }
        else if (_status == PacketStatus.ThirdHeaderReceived || _status == PacketStatus.ReceivingPacketLenght)
        {
            const int sizeOfInt32 = sizeof (int);
            const int sizeOfByte = sizeof (byte);

            _packetLength |= b &lt;&lt; (sizeOfInt32 - ++_packetLenghtOffset) * sizeOfByte;
            _status = _packetLenghtOffset &lt; 4 ? PacketStatus.ReceivingPacketLenght : PacketStatus.ReceivingData;
        }
        else if (_status == PacketStatus.ReceivingData)
        {
            _packet.Add(b);
            var receivedByteCount = _packet.Count;
            if (receivedByteCount == _packetLength)
            {
                var packetData = new byte[_packet.Count];
                _packet.CopyTo(packetData);

                var receivedPacketHandler = PacketReceived;
                if(receivedPacketHandler != null)
                {
                    receivedPacketHandler(this, new PacketReceivedEventArgs{ Packet = packetData });
                }
                ResetControlVariables();
            }
        }
    }
Simple pattern: Set up a hash table or map that maps the incoming byte to a function that returns the correct response.

(C++ pseudocode - can be done in C# as well - don't have time now for all the implementation details:)

__statusType responseFunc(_statusType,byte);

map&lt;byte,responseFunc&gt; responseMap;

private void ProcessByte(byte b)

{
  _status= responseMap[_status](_status,b);
}


Although you have some nested conditions, etc, your handler functions can deal with those conditions - but you may have to rethink your design and set up some classes that properly represent and understand your conditions. A long switch case or 'if else if' is invariably the result of poor design. You need to think carefully about your architecture. 

Another thing to consider is abstracting your conditions a bit and setting up an enumeration or class hierarchy to represent them. They can then be used as the key for your map or a parameter to your handler functions. 

Yes - this is work - but clean design does require some effort. If you don't like long conditional statements (and you shouldn't) put in the effort to clean them up. 

You should think along those lines - maps, dictionaries, enumerations, class hierarchies, factories, etc - whenever you run into a situation that seems to require an extended 'if else if..' or 'switch case';