socket_wrapper: correctly handle dup()/dup2() ref counting