Rewrite the protocol engine

In particular; handle communication in a separate thread. Not all
pieces are in place quite yet.