You could perhaps save the time when the GPIO input changes... and wait to do the DigitalWrite until some timeout (a second?). If you receive the ack from gateway, just reset the timeout and do exactly like you do today.