@@ -2238,7 +2238,11 @@ class PythonUserSocketStream : Stream {
                 get { return true; }
             }
 
-            public override void Close() { Dispose(false); }
+            public override void Close() {
+                if (PythonOps.HasAttr(DefaultContext.Default,_userSocket,"close"))
+                    DefaultContext.DefaultPythonContext.CallSplat(PythonOps.GetBoundAttr(DefaultContext.Default, _userSocket, "close"));
+                Dispose(false); 
+            }
 
             public override void Flush() {
                 if (_data.Count > 0) {
@@ -2313,10 +2317,14 @@ public _fileobject(CodeContext/*!*/ context, object socket, [DefaultParameterVal
                     _socket = s;
                     stream = new NetworkStream(s._socket, false);
                 } else {
+                    _socket = null;
                     stream = new PythonUserSocketStream(socket, GetBufferSize(context, bufsize), close);
                 }
-                _isOpen = true;
+               
                 base.__init__(stream, System.Text.Encoding.Default, mode);
+
+                _isOpen = (socket == null) ? false : true;
+                _close = (socket == null) ? false : close;
             }
 
             public void __init__(params object[] args) {
@@ -2342,7 +2350,12 @@ public _fileobject(CodeContext/*!*/ context, object socket, [DefaultParameterVal
 
             public override object close() {
                 if (!_isOpen) return null;
-                if (_socket != null && _close) _socket.close();
+                if (_socket != null && _close) {
+                    _socket.close();
+                }
+                else if (this._stream != null && _close) {
+                    _stream.Close();
+                }
                 _isOpen = false;
                 var obj = base.close();
                 return obj;