It doesn't fail to DNS the hostname at all. You're using SOCKS5, so it doesn't have to DNS the hostname. The problem is, it isn't DNSing the hostname. How is mIRC supposed to know $serverip if it doesn't DNS the hostname or the SOCKS5 proxy doesn't tell it... iirc the SOCKS5 proxy doesn't have to tell it accurately either.

The following is a rough procedure that describes the way mIRC appears to be connecting via SOCKS5 (and HTTPS?):
1. Connect to the proxy server.
2. Authenticate.
3. Connect the proxy server to the hostname provided.

The modification that's required to accurately fill in $serverip follows the same algorithm as the SOCKS4 connections:
1. DNS the hostname.
2. Pick an IP. Save it as $serverip.
3. Connect to the proxy server.
4. Authenticate.
5. Connect the proxy server to $serverip.
In reality, steps 1-2 and 3-4 could be performed independently, so they could be swapped or performed on different threads.