extract common code used in client/server