Just a quick note:
I found socket_getsockname() is not IPv6 compatible or may just return some unexpected results?
Simple Code Example:
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_getsockname($socket, $IP, $PORT);
This does not print $IP 127.0.0.1 or 192.168.1.1 or even 0.0.0.0 ... etc ... like you would expect ... in my case I receive 10.0.0.0 witch is not a valid port on my system using IPv5 and IPv6! Thus you should rely on using the initial values of socket_bind() to get the local address.
Hope this helps...