Backporting gateway response code improvements from 1.3 to 1.2.