Add serialization methods for wire protocol

Repushing commit from last pull request (now ea31c86) with William's suggested changes. (GitHub strongly dislikes amended forced commits)