fix bugs in gateway.c