QPID-6475: [Python Client] 08..09 Send connection.close before closing socket
diff --git a/qpid/client.py b/qpid/client.py
index 7ae0106..1bb225e 100644
--- a/qpid/client.py
+++ b/qpid/client.py
@@ -127,7 +127,17 @@
 
   def close(self):
     if self.peer:
-      self.peer.stop()
+      try:
+        if not self.closed:
+          channel = self.channel(0);
+          if channel and not channel._closed:
+             try:
+               channel.connection_close(reply_code=200)
+             except:
+               pass
+          self.closed = True
+      finally:
+        self.peer.stop()
 
 class ClientDelegate(Delegate):
 
diff --git a/qpid/testlib.py b/qpid/testlib.py
index 0103956..841dcfb 100644
--- a/qpid/testlib.py
+++ b/qpid/testlib.py
@@ -71,10 +71,7 @@
             print "Error on tearDown:"
             print traceback.print_exc()
 
-        if not self.client.closed:
-            self.client.channel(0).connection_close(reply_code=200)
-        else:
-            self.client.close()
+        self.client.close()
 
     def connect(self, host=None, port=None, user=None, password=None, tune_params=None, client_properties=None):
         """Create a new connction, return the Client object"""