Refactored msg.Encode methods to return int, error matching the Write interface.

This was done to enable gathering of metrics for msg length.