It seems that you are saying that using SOCKS by nature means DNS won't work.
If this was the case, when connecting directly to the server ip, eg: 66.225.225.225, $serverip would return 66.225.225.225, because it wouldn't need to resolve the server hostname.
However, it does not, it returns 255.255.255.255.
I'm not convinced the issue has been properly identified.
I am still interested to know exactly where $serverip pulls the ip address from.