--- lib/Qpsmtpd/TcpServer.bak   2008-09-23 12:47:16.000000000 -0500
+++ lib/Qpsmtpd/TcpServer.pm    2008-09-23 14:20:49.727410750 -0500
@@ -26,12 +26,15 @@
 }
 
 my $first_0; 
+my $client_socket;
 
 sub start_connection {
     my $self = shift;
 
     my ($remote_host, $remote_info, $remote_ip);
 
+    $client_socket = 0;
+
     if ($ENV{TCPREMOTEIP}) {
        # started from tcpserver (or some other superserver which
        # exports the TCPREMOTE* variables.
@@ -67,7 +70,10 @@
 }
 
 sub run {
-    my $self = shift;
+    my ($self, $client) = @_;
+
+    # Set local client_socket to passed client object for testing socket state on writes
+    $client_socket = $client;
 
     # should be somewhere in Qpsmtpd.pm and not here...
     $self->load_plugins unless $self->{hooks};
@@ -76,7 +82,6 @@
     return if $rc != DONE;
 
     # this should really be the loop and read_input should just get one line; I think
-
     $self->read_input;
 }
 
@@ -104,6 +109,12 @@
 sub respond {
   my ($self, $code, @messages) = @_;
   my $buf = '';
+
+  if ( !$self->check_socket() ) {
+    $self->log(LOGERROR, "Lost connection to client, cannot send response.");
+    return(0);
+  }
+
   while (my $msg = shift @messages) {
     my $line = $code . (@messages?"-":" ").$msg;
     $self->log(LOGINFO, $line);
@@ -161,4 +172,12 @@
   return ($TCPLOCALIP, $TCPREMOTEIP, $TCPREMOTEHOST || "Unknown");
 }
 
+sub check_socket() {
+  my $self = shift;
+
+  return 1 if ( $client_socket->connected );
+ 
+  return 0;
+}
+
 1;
--- lib/Qpsmtpd/TcpServer/Prefork.bak   2008-09-23 12:48:01.000000000 -0500
+++ lib/Qpsmtpd/TcpServer/Prefork.pm    2008-09-23 14:01:16.402082500 -0500
@@ -48,6 +48,12 @@
 
 sub respond {
   my ($self, $code, @messages) = @_;
+
+  if ( !$self->check_socket() ) {
+    $self->log(LOGERROR, "Lost connection to client, cannot send response.");
+    return(0);
+  }
+
   while (my $msg = shift @messages) {
     my $line = $code . (@messages?"-":" ").$msg;
     $self->log(LOGINFO, $line);
--- qpsmtpd-prefork.bak 2008-09-23 12:46:21.000000000 -0500
+++ qpsmtpd-prefork     2008-09-23 13:19:27.613435750 -0500
@@ -652,7 +652,7 @@
                                    remote_ip   => $ENV{TCPREMOTEIP},
                                    remote_port => $client->peerport,
                                   );
-        $qpsmtpd->run();
+        $qpsmtpd->run($client);
         $qpsmtpd->run_hooks("post-connection");
         $qpsmtpd->connection->reset;
     };
