Refactor server/client mode switch code