diff --git a/lib/net.js b/lib/net.js index 751b04b05e..84c68330c1 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1083,8 +1083,16 @@ var createServerHandle = exports._createServerHandle = } if (address || port) { - debug('bind to ' + address); - if (addressType === 6) { + debug('bind to ' + (address || 'anycast')); + if (!address) { + // Try binding to ipv6 first + err = handle.bind6('::', port); + if (err) { + handle.close(); + // Fallback to ipv4 + return createServerHandle('0.0.0.0', port); + } + } else if (addressType === 6) { err = handle.bind6(address, port); } else { err = handle.bind(address, port); @@ -1214,7 +1222,7 @@ Server.prototype.listen = function() { if (arguments.length == 0 || util.isFunction(arguments[0])) { // Bind to a random port. - listen(self, '0.0.0.0', 0, null, backlog); + listen(self, null, 0, null, backlog); } else if (arguments[0] && util.isObject(arguments[0])) { var h = arguments[0]; @@ -1240,7 +1248,7 @@ Server.prototype.listen = function() { util.isFunction(arguments[1]) || util.isNumber(arguments[1])) { // The first argument is the port, no IP given. - listen(self, '0.0.0.0', port, 4, backlog); + listen(self, null, port, 4, backlog); } else { // The first argument is the port, the second an IP. @@ -1248,7 +1256,7 @@ Server.prototype.listen = function() { if (err) { self.emit('error', err); } else { - listen(self, ip || '0.0.0.0', port, ip ? addressType : 4, backlog); + listen(self, ip, port, ip ? addressType : 4, backlog); } }); }