remove functions for perf boost

Use a little state machine instead of the nested functions.

The tests pass, as do framing-socket's.