Move implementation of the master-slave protocol to procotol.c.