common: move code from client.c