So I'm refactoring a very ugly piece of client code that needs to
implement some fairly complicated error correction over a line based
tcp protocol.  There are about 10 different error scenarios I have to
detect and respond differently to.  Think error correction over serial
lines, that's actually where this was derived from and now it's
layered over a tcp connection.  Most of the scenarios involve several
back and forth messages between client and host.  Most of the
scenarios are very similar, so if you think of it as a tree I might
not know what scenario I am dealing with until I've gotten 2-3 levels
deep, and at any point in the tree I need to know where I am and what
the possible branches are I can take based on the next response from
the host.

Any suggestions on how to implement this cleanly?

Chris