diff --git a/src/Renci.SshNet/Abstractions/SocketAbstraction.cs b/src/Renci.SshNet/Abstractions/SocketAbstraction.cs
index c26ab9f7..43865f38 100644
--- a/src/Renci.SshNet/Abstractions/SocketAbstraction.cs
+++ b/src/Renci.SshNet/Abstractions/SocketAbstraction.cs
@@ -25,9 +25,17 @@ namespace Renci.SshNet.Abstractions
}
+ ///
+ /// Returns a value indicating whether the specified can be used
+ /// to send data.
+ ///
+ /// The to check.
+ ///
+ /// true if can be written to; otherwise, false.
+ ///
public static bool CanWrite(Socket socket)
{
- if (socket.Connected)
+ if (socket != null && socket.Connected)
{
#if FEATURE_SOCKET_POLL
return socket.Poll(-1, SelectMode.SelectWrite);
diff --git a/src/Renci.SshNet/Session.cs b/src/Renci.SshNet/Session.cs
index bf99dd26..06fce659 100644
--- a/src/Renci.SshNet/Session.cs
+++ b/src/Renci.SshNet/Session.cs
@@ -834,7 +834,7 @@ namespace Renci.SshNet
/// The size of the packet exceeds the maximum size defined by the protocol.
internal void SendMessage(Message message)
{
- if (_socket == null || !_socket.CanWrite())
+ if (!_socket.CanWrite())
throw new SshConnectionException("Client not connected.");
if (_keyExchangeInProgress && !(message is IKeyExchangedAllowed))
@@ -893,7 +893,7 @@ namespace Renci.SshNet
}
// increment the packet sequence number only after we're sure the packet has
- // been sent; even though it's only used for the MAC, it need to be incremented
+ // been sent; even though it's only used for the MAC, it needs to be incremented
// for each package sent.
//
// the server will use it to verify the data integrity, and as such the order in
diff --git a/test/Renci.SshNet.Shared.Tests/Abstractions/SocketAbstraction_CanWrite.cs b/test/Renci.SshNet.Shared.Tests/Abstractions/SocketAbstraction_CanWrite.cs
new file mode 100644
index 00000000..c9e56cb7
--- /dev/null
+++ b/test/Renci.SshNet.Shared.Tests/Abstractions/SocketAbstraction_CanWrite.cs
@@ -0,0 +1,24 @@
+using System.Net.Sockets;
+using Renci.SshNet.Abstractions;
+#if SILVERLIGHT
+using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
+#else
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+#endif
+
+namespace Renci.SshNet.Tests.Abstractions
+{
+ [TestClass]
+ public class SocketAbstraction_CanWrite
+ {
+ [TestMethod]
+ public void ShouldReturnFalseWhenSocketIsNull()
+ {
+ const Socket socket = null;
+
+ var actual = SocketAbstraction.CanWrite(socket);
+
+ Assert.IsFalse(actual);
+ }
+ }
+}
diff --git a/test/Renci.SshNet.Shared.Tests/Renci.SshNet.Shared.Tests.projitems b/test/Renci.SshNet.Shared.Tests/Renci.SshNet.Shared.Tests.projitems
index 0fc2aa43..7539d384 100644
--- a/test/Renci.SshNet.Shared.Tests/Renci.SshNet.Shared.Tests.projitems
+++ b/test/Renci.SshNet.Shared.Tests/Renci.SshNet.Shared.Tests.projitems
@@ -12,6 +12,7 @@
+