private void WriteCallback(IAsyncResult result) { var asyncData = (AsyncData)result.AsyncState; try { _stream.EndWrite(result); // send data sent event OnDataLengthSent(new DataLengthEventArgs(asyncData.Buffer.Length)); // send status message OnStatusMessage(new StatusMessageEventArgs(String.Format(CultureInfo.CurrentCulture, "Sent command: {0} ({1} bytes)", CleanUpCommandText(asyncData.Command), asyncData.Buffer.Length), TraceLevel.Info)); } catch (Exception ex) { OnStatusMessage(new StatusMessageEventArgs(String.Format(CultureInfo.CurrentCulture, "Write failed: {0}", ex.Message), TraceLevel.Error)); Close(); } }