Improved Gateway error handling

* General improvements to reduce the case where something bad happens
  and RDP hangs around forever without closing.
* Specific error code improvement for CAP related login rejection.
* May fix #1589