The code below will fail because Bind() is called on a socket that has not been "prepared", even though there is code to prepare the socket. The code that prepares the socket is out of scope (another Try block).
// prepare socket
try
{
socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}
catch (Exception e)
{
log.write("socket preparation failed");
}
finally
{
if (socket != null)
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
// bind
try
{
socket.Bind(endPoint);
}
catch (Exception e)
{
log.write("Bind() failed");
}
finally
{
if (socket != null)
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
// enable listening
try
{
socket.Listen(1000);
}
catch (Exception e)
{
log.write("Listen() failed");
}
finally
{
if (socket != null)
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}