Complete rewrite with correct gateway and protocol implementation.

Just works right now.