protected virtual void Dispose(bool disposing)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
if (_cleanedUp)
{
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
return;
}
if (disposing)
{
IDisposable dataStream = _dataStream;
if (dataStream != null)
{
dataStream.Dispose();
}
else
{
// If the NetworkStream wasn't created, the Socket might
// still be there and needs to be closed. In the case in which
// we are bound to a local IPEndPoint this will remove the
// binding and free up the IPEndPoint for later uses.
Socket chkClientSocket = _clientSocket;
if (chkClientSocket != null)
{
try
{
chkClientSocket.InternalShutdown(SocketShutdown.Both);
}
finally
{
chkClientSocket.Dispose();
_clientSocket = null;
}
}
}
DisposeCore(); // platform-specific disposal work
GC.SuppressFinalize(this);
}
_cleanedUp = true;
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
}