public void SendError(string msg, int status) { try { HttpListenerResponse response = context.Response; response.StatusCode = status; response.ContentType = "text/html"; string description = HttpListenerResponse.GetStatusDescription(status); string str; if (msg != null) { str = String.Format("<h1>{0} ({1})</h1>", description, msg); } else { str = String.Format("<h1>{0}</h1>", description); } byte[] error = context.Response.ContentEncoding.GetBytes(str); response.Close(error, false); } catch { // response was already closed } }