r = (++client_cookie_num) % 65535;

do the increment and the mod in one line. much clearer.