Add more tests.
Implement protocol switching.
Refactor some shared code; remove useless code.