server main loop code