There are many variables here ... but I came across the same behavior with the remote cgi script that I was working on.
In my case, the problem was related to SELinux on CentOS.
user@remoteserver:~$ getsebool -a | grep httpd
Showed:
...
httpd_can_network_connect --> off
...
Check for a possible fix (sudo or run as root):
user@remoteserver:~$ setsebool httpd_can_network_connect=1
Permanent correction (if proven above):
user@remoteserver:~$ setsebool -P httpd_can_network_connect=1
Option-P , SELinux .
:
man getsebool
man setsebool