Merge pull request #52 from HavretGC/AMQNET-633_Cannot-create-Session-when-Connection-not-started

AMQNET-633: Fix session creation when connection not started
diff --git a/src/NMS.AMQP/NmsConnection.cs b/src/NMS.AMQP/NmsConnection.cs
index 6ce0903..0d637e5 100644
--- a/src/NMS.AMQP/NmsConnection.cs
+++ b/src/NMS.AMQP/NmsConnection.cs
@@ -416,24 +416,28 @@
 
             lock (syncRoot)
             {
-                if (!closed && connected.CompareAndSet(false, true))
+                if (closed || connected)
+                {
+                    return;
+                }
+                
+                try
+                {
+                    provider.Connect(ConnectionInfo).ConfigureAwait(false).GetAwaiter().GetResult();
+                    connected.Set(true);
+                }
+                catch (Exception e)
                 {
                     try
                     {
-                        provider.Connect(ConnectionInfo).ConfigureAwait(false).GetAwaiter().GetResult();
+                        provider.Close();
                     }
-                    catch (Exception e)
+                    catch
                     {
-                        try
-                        {
-                            provider.Close();
-                        }
-                        catch
-                        {
-                        }
-
-                        throw NMSExceptionSupport.Create(e);
+                        // ignored
                     }
+
+                    throw NMSExceptionSupport.Create(e);
                 }
             }
         }
diff --git a/test/Apache-NMS-AMQP-Interop-Test/NmsSessionTest.cs b/test/Apache-NMS-AMQP-Interop-Test/NmsSessionTest.cs
new file mode 100644
index 0000000..849f684
--- /dev/null
+++ b/test/Apache-NMS-AMQP-Interop-Test/NmsSessionTest.cs
@@ -0,0 +1,22 @@
+using System.Threading.Tasks;
+using Apache.NMS;
+using NUnit.Framework;
+
+namespace NMS.AMQP.Test
+{
+    public class NmsSessionTest : AmqpTestSupport
+    {
+        [Test, Timeout(10_000)]
+        public void TestCreateMultipleSessionsFromDifferentThreadsWhenConnectionNotStarted()
+        {
+            Connection = CreateAmqpConnection();
+            Assert.NotNull(Connection);
+
+            Parallel.For(0, 10, i =>
+            {
+                ISession session = Connection.CreateSession();
+                Assert.NotNull(session);
+            });
+        }
+    }
+}
\ No newline at end of file