 modules/juce_core/network/juce_Socket.cpp | 30 ++++++++++++++++++++++--------
 modules/juce_core/network/juce_Socket.h   |  1 +
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/modules/juce_core/network/juce_Socket.cpp b/modules/juce_core/network/juce_Socket.cpp
index 8196391e3..87fdd8fde 100644
--- a/modules/juce_core/network/juce_Socket.cpp
+++ b/modules/juce_core/network/juce_Socket.cpp
@@ -87,7 +87,7 @@ namespace SocketHelpers
 
     static bool resetSocketOptions (SocketHandle handle, bool isDatagram, bool allowBroadcast) noexcept
     {
-        return handle > 0
+        return handle >= 0
                 && setOption (handle, SO_RCVBUF, (int) 65536)
                 && setOption (handle, SO_SNDBUF, (int) 65536)
                 && (isDatagram ? ((! allowBroadcast) || setOption (handle, SO_BROADCAST, (int) 1))
@@ -103,8 +103,8 @@ namespace SocketHelpers
        #if JUCE_WINDOWS
         ignoreUnused (portNumber, isListener, readLock);
 
-        if (h != (unsigned) SOCKET_ERROR || connected)
-            closesocket (h);
+        if (h != invalidSocket || connected)
+            closesocket (h);;
 
         // make sure any read process finishes before we delete the socket
         CriticalSection::ScopedLockType lock (readLock);
@@ -147,7 +147,7 @@ namespace SocketHelpers
 
     static bool bindSocket (SocketHandle handle, int port, const String& address) noexcept
     {
-        if (handle <= 0 || ! isValidPortNumber (port))
+        if (handle < 0 || ! isValidPortNumber (port))
             return false;
 
         struct sockaddr_in addr;
@@ -163,7 +163,7 @@ namespace SocketHelpers
 
     static int getBoundPort (SocketHandle handle) noexcept
     {
-        if (handle > 0)
+        if (handle >= 0)
         {
             struct sockaddr_in addr;
             socklen_t len = sizeof (addr);
@@ -514,8 +514,9 @@ bool StreamingSocket::connect (const String& remoteHostName, int remotePortNumbe
 
     connected = SocketHelpers::connectSocket (handle, readLock, remoteHostName,
                                               remotePortNumber, timeOutMillisecs);
-
-    if (! (connected && SocketHelpers::resetSocketOptions (handle, false, false)))
+    if (!connected)
+      return false;
+    if (! SocketHelpers::resetSocketOptions (handle, false, false))
     {
         close();
         return false;
@@ -526,7 +527,8 @@ bool StreamingSocket::connect (const String& remoteHostName, int remotePortNumbe
 
 void StreamingSocket::close()
 {
-    SocketHelpers::closeSocket (handle, readLock, isListener, portNumber, connected);
+    if (handle >= 0)
+      SocketHelpers::closeSocket (handle, readLock, isListener, portNumber, connected);
 
     hostName.clear();
     portNumber = 0;
@@ -604,6 +606,7 @@ bool StreamingSocket::isLocal() const noexcept
 //==============================================================================
 //==============================================================================
 DatagramSocket::DatagramSocket (bool canBroadcast)
+  : allowBroadcast(canBroadcast)
 {
     SocketHelpers::initSockets();
 
@@ -633,6 +636,7 @@ void DatagramSocket::shutdown()
     handle = -1;
     std::atomic<bool> connected { false };
     SocketHelpers::closeSocket (handleCopy, readLock, false, 0, connected);
+    isBound = false;
 }
 
 bool DatagramSocket::bindToPort (int port)
@@ -644,6 +648,16 @@ bool DatagramSocket::bindToPort (int port, const String& addr)
 {
     jassert (SocketHelpers::isValidPortNumber (port));
 
+    if (handle < 0) // socket has been previously shutdown
+    {
+      handle = (int)socket(AF_INET, SOCK_DGRAM, 0);
+      if (handle >= 0)
+      {
+        SocketHelpers::resetSocketOptions(handle, true, allowBroadcast);
+        SocketHelpers::makeReusable(handle);
+      }
+   }
+   
     if (SocketHelpers::bindSocket (handle, port, addr))
     {
         isBound = true;
diff --git a/modules/juce_core/network/juce_Socket.h b/modules/juce_core/network/juce_Socket.h
index 524554ee5..93287dbb8 100644
--- a/modules/juce_core/network/juce_Socket.h
+++ b/modules/juce_core/network/juce_Socket.h
@@ -352,6 +352,7 @@ private:
     int lastServerPort = -1;
     void* lastServerAddress = nullptr;
     mutable CriticalSection readLock;
+    bool allowBroadcast;
 
     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DatagramSocket)
 };
