Sniffing the traffic between the PABX and a commercial call logger reveals that the PABX frequently drops the connection for no good reason and any app wishing to log call data must monitor the state of the connection.
Since TcpClient won't detect a change of state until a write fails and this is strictly a listening socket, a more direct approach to connection status is required:
public static class Extensions
{
/// <summary>
/// Obtain the current state of the socket underpinning a TcpClient.
/// Unlike TcpClient.Connected this is reliable and does not require
/// capture of an exception resulting from a failed socket write.
/// </summary>
/// <param name="tcpClient">TcpClient instance</param>
/// <returns>Current state of the TcpClient.Client socket</returns>
public static TcpState GetState(this TcpClient tcpClient)
{
var foo = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections()
.SingleOrDefault(x => x.LocalEndPoint.Equals(tcpClient.Client.LocalEndPoint));
return foo != null ? foo.State : TcpState.Unknown;
}
}
It is also necessary to handle redundant call logs since these are often repeated when the connection is re-established.