protected static void configureMulticastSocket(MulticastSocket multicastSocket, Address bindAddress,
            HazelcastProperties hzProperties, MulticastConfig multicastConfig, ILogger logger)
            throws SocketException, IOException, UnknownHostException {
        multicastSocket.setReuseAddress(true);
        // bind to receive interface
        multicastSocket.bind(new InetSocketAddress(multicastConfig.getMulticastPort()));
        multicastSocket.setTimeToLive(multicastConfig.getMulticastTimeToLive());
        try {
            boolean loopbackBind = bindAddress.getInetAddress().isLoopbackAddress();
            Boolean loopbackModeEnabled = multicastConfig.getLoopbackModeEnabled();
            if (loopbackModeEnabled != null) {
                // setting loopbackmode is just a hint - and the argument means "disable"!
                // to check the real value we call getLoopbackMode() (and again - return value means "disabled")
                multicastSocket.setLoopbackMode(!loopbackModeEnabled);
            }
            // If LoopBack mode is not enabled (i.e. getLoopbackMode return true) and bind address is a loopback one,
            // then print a warning
            if (loopbackBind && multicastSocket.getLoopbackMode()) {
                logger.warning("Hazelcast is bound to " + bindAddress.getHost() + " and loop-back mode is "
                        + "disabled. This could cause multicast auto-discovery issues "
                        + "and render it unable to work. Check your network connectivity, try to enable the "
                        + "loopback mode and/or force -Djava.net.preferIPv4Stack=true on your JVM.");
            }

            // warning: before modifying lines below, take a look at these links:
            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4417033
            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6402758
            // https://github.com/hazelcast/hazelcast/pull/19251#issuecomment-891375270
            boolean callSetInterface = OS.isMac() || !loopbackBind;
            String propSetInterface = hzProperties.getString(ClusterProperty.MULTICAST_SOCKET_SET_INTERFACE);
            if (propSetInterface != null) {
                callSetInterface = Boolean.parseBoolean(propSetInterface);
            }
            if (callSetInterface) {
                multicastSocket.setInterface(bindAddress.getInetAddress());
            }
        } catch (Exception e) {
            logger.warning(e);
        }
        multicastSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
        multicastSocket.setSendBufferSize(SOCKET_BUFFER_SIZE);
        String multicastGroup = hzProperties.getString(ClusterProperty.MULTICAST_GROUP);
        if (multicastGroup == null) {
            multicastGroup = multicastConfig.getMulticastGroup();
        }
        multicastConfig.setMulticastGroup(multicastGroup);
        multicastSocket.joinGroup(InetAddress.getByName(multicastGroup));
        multicastSocket.setSoTimeout(SOCKET_TIMEOUT);
    }
@Test
    public void testSetInterfaceDefaultWhenLoopback() throws Exception {
        Config config = createConfig(null);
        MulticastConfig multicastConfig = config.getNetworkConfig().getJoin().getMulticastConfig();
        multicastConfig.setLoopbackModeEnabled(true);
        MulticastSocket multicastSocket = mock(MulticastSocket.class);
        Address address = new Address("127.0.0.1",  5701);
        HazelcastProperties hzProperties = new HazelcastProperties(config);
        MulticastService.configureMulticastSocket(multicastSocket, address, hzProperties , multicastConfig, mock(ILogger.class));
        verify(multicastSocket).setLoopbackMode(false);
        // https://github.com/hazelcast/hazelcast/pull/19251#issuecomment-891375270
        if (OS.isMac()) {
            verify(multicastSocket).setInterface(address.getInetAddress());
        } else {
            verify(multicastSocket, never()).setInterface(any());
        }
    }