Rewriting.

Since we were just prototyping, it's time to rewrite the code.

Most of the protocol-related code will be factored out so that they
could be used both in local and remote side.