Index: rkward/scriptbackends/phpbackend.cpp
===================================================================
--- rkward/scriptbackends/phpbackend.cpp	(revision 2535)
+++ rkward/scriptbackends/phpbackend.cpp	(working copy)
@@ -59,22 +59,9 @@
 		return false;
 	}
 
-	QDir files_path (RKCommonFunctions::getRKWardDataDir () + "phpfiles/");
-	QString common_php = files_path.absoluteFilePath ("common.php");
-	QString php_ini = files_path.absoluteFilePath ("php.ini");	// make sure to set good options
-	if (!QFileInfo (common_php).isReadable ()) {
-		KMessageBox::error (0, i18n ("The support file \"%1\" could not be found or is not readable. Please check your installation.", common_php), i18n ("PHP-Error"));
-		emit (haveError ());
-		return false;
-	}
+	php_process = PHPProcessManager::requestPHPProcess (this);
+	connect (php_process, SIGNAL (readyRead()), this, SLOT(gotOutput()));
 
-	php_process = new QProcess (this);
-	connect (php_process, SIGNAL (readyRead()), this, SLOT (gotOutput()));
-	connect (php_process, SIGNAL (error(QProcess::ProcessError)), this, SLOT (processError(QProcess::ProcessError)));
-	connect (php_process, SIGNAL (finished(int,QProcess::ExitStatus)), this, SLOT (processDead(int,QProcess::ExitStatus)));
-
-	php_process->start ("\"" + RKSettingsModulePHP::phpBin() + "\"" + " -c \"" + php_ini + "\" \"" + common_php + "\"");
-
 	doing_command = startup_done = false;
 	busy = true;
 
@@ -90,7 +77,7 @@
 	RK_TRACE (PHP);
 	if (!dead) {
 		dead = true;
-		php_process->kill ();
+		php_process->abort ();
 		QTimer::singleShot (10000, this, SLOT (deleteLater()));	// don't wait for ever for the process to die, even if it's somewhat dangerous
 	}
 
@@ -104,7 +91,7 @@
 void PHPBackend::tryNextFunction () {
 	RK_TRACE (PHP);
 
-	if (php_process && (php_process->state () == QProcess::Running) && (!busy) && (!command_stack.isEmpty ())) {
+	if (php_process && (php_process->state () == QAbstractSocket::ConnectedState) && (!busy) && (!command_stack.isEmpty ())) {
 	/// clean up previous command if applicable
 		if (command_stack.first ()->complete) {
 			delete command_stack.takeFirst ();
@@ -114,6 +101,7 @@
 		
 		RK_DO (qDebug ("submitting PHP code: %s", command_stack.first ()->command.toLatin1 ().data ()), PHP, DL_DEBUG);
 		php_process->write (QString (command_stack.first ()->command + eot_string).toLatin1 ());
+		php_process->flush ();
 		doing_command = busy = true;
 		command_stack.first ()->complete = true;
 		current_flags = command_stack.first ()->flags;
@@ -126,12 +114,12 @@
 
 	RK_DO (qDebug ("submitting data: %s", data.toLatin1 ().data ()), PHP, DL_DEBUG);
 	php_process->write (QString (data + eot_string).toLatin1 ());
+	php_process->flush ();
 	tryNextFunction ();
 }
 
 void PHPBackend::gotOutput () {
 	RK_TRACE (PHP);
-
 	qint64 len = php_process->bytesAvailable ();
 	if (!len) return;	// false alarm
 
@@ -207,6 +195,7 @@
 	}
 }
 
+/*
 void PHPBackend::processError (QProcess::ProcessError error) {
 	RK_TRACE (PHP);
 
@@ -232,5 +221,79 @@
 		deleteLater ();
 	} else destroy ();
 }
+*/
 
+
+////////////////////
+#include <QCoreApplication>
+PHPProcessManager *PHPProcessManager::php_process_manager = 0;
+
+QAbstractSocket* PHPProcessManager::requestPHPProcess (PHPBackend *client) {
+	RK_TRACE (PHP);
+
+	if (!php_process_manager) {
+		new PHPProcessManager (qApp);
+		if (!php_process_manager) return 0;
+	}
+
+	QTcpSocket *socket = new QTcpSocket (client);
+	socket->connectToHost ("127.0.0.1", php_process_manager->port);
+
+	// socket->write (shared_secret);
+
+	return socket;
+}
+
+PHPProcessManager::PHPProcessManager (QObject *parent) : QObject (parent) {
+	RK_TRACE (PHP);
+qDebug ("starting");
+	QDir files_path (RKCommonFunctions::getRKWardDataDir () + "phpfiles/");
+	QString common_php = files_path.absoluteFilePath ("common.php");
+	QString php_ini = files_path.absoluteFilePath ("php.ini");	// make sure to set good options
+	if (!QFileInfo (common_php).isReadable ()) {
+		KMessageBox::error (0, i18n ("The support file \"%1\" could not be found or is not readable. Please check your installation.", common_php), i18n ("PHP-Error"));
+//		emit (haveError ());
+//		return false;
+		deleteLater ();
+		return;
+	}
+
+	php_process = new QProcess (this);
+//	connect (php_process, SIGNAL (finished(int,QProcess::ExitStatus)), this, SLOT (processDead(int,QProcess::ExitStatus)));
+
+	php_process->start ("\"" + RKSettingsModulePHP::phpBin() + "\"" + " -c \"" + php_ini + "\" \"" + common_php + "\"");
+
+	bool ok = false;
+	while (!ok) {
+		ok = php_process->waitForReadyRead (200);
+		qApp->processEvents ();
+	}
+
+	QByteArray buf = php_process->readAll ();
+	QString output = QString::fromLatin1 (buf);
+
+	QStringList list = output.split (';');
+	if (list.length () == 2) {
+		port = list[0].toInt ();
+		shared_secret = list[1];
+qDebug ("%d, %s", port, qPrintable (shared_secret));
+
+		php_process_manager = this;
+	} else {
+		deleteLater ();
+	}
+
+connect (php_process, SIGNAL (readyRead()), this, SLOT (gotOutput()));
+}
+
+PHPProcessManager::~PHPProcessManager () {
+	RK_TRACE (PHP);
+qDebug ("dying");
+}
+
+void PHPProcessManager::gotOutput () {
+	QByteArray buf = php_process->readAll ();
+qDebug ("PHP: %s", buf.data ());
+}
+
 #include "phpbackend.moc"
Index: rkward/scriptbackends/phpbackend.h
===================================================================
--- rkward/scriptbackends/phpbackend.h	(revision 2501)
+++ rkward/scriptbackends/phpbackend.h	(working copy)
@@ -23,6 +23,7 @@
 #include <qobject.h>
 #include <QProcess>
 #include <qstringlist.h>
+#include <QTcpSocket>
 
 class RKPlugin;
 
@@ -48,11 +49,9 @@
 	void writeData (const QString &data);
 public slots:
 	void gotOutput ();
-	void processError (QProcess::ProcessError error);
-	void processDead (int exitCode, QProcess::ExitStatus exitStatus);
 private:
 	void tryNextFunction ();
-	QProcess *php_process;
+	QAbstractSocket *php_process;
 /// The string singalling the end of transmission to/from PHP. TODO: make static
 	QString eot_string;
 /// The string singalling the end of request from PHP. TODO: make static
@@ -67,4 +66,22 @@
 	QString filename;
 };
 
+
+class PHPProcessManager : public QObject {
+	Q_OBJECT
+public:
+	static QAbstractSocket* requestPHPProcess (PHPBackend *client);
+public slots:
+	void gotOutput ();
+protected:
+	PHPProcessManager (QObject *parent);
+	~PHPProcessManager ();
+
+	int port;
+	QString shared_secret;
+	QProcess *php_process;
+
+	static PHPProcessManager *php_process_manager;
+};
+
 #endif
Index: rkward/scriptbackends/common.php
===================================================================
--- rkward/scriptbackends/common.php	(revision 2504)
+++ rkward/scriptbackends/common.php	(working copy)
@@ -40,11 +40,15 @@
 }
 
 function getInput ($prompt) {
-	fputs (STDOUT, "#RKEND#\n" . $prompt . "#RKQEND#\n");
-	fflush (STDOUT);
+	global $_rk_client_socket;
+	write_to_socket ("#RKEND#\n" . $prompt . "#RKQEND#\n");
 	while (1) {
-		if (feof (STDIN)) die ("STDIN is at eof");			// if the parent process exits unexpectedly, make sure the PHP-process gets killed
-		$inp = fgets (STDIN, 4096);
+		$inp = socket_read ($_rk_client_socket, 4096);
+		if (($inp === '') || ($inp === false)) {
+			// if the parent process exits unexpectedly, make sure the PHP-process gets killed
+			// according to the main docs, we get socket_read returns false in that case, but apparently, it really is ''
+			die ();
+		}
 		if ($inp != "") {
 			$input .= $inp;
 			$inp = "";
@@ -59,8 +63,50 @@
 ini_set ("error_prepend_string", "#RKEND#\nPHP-Error");
 ini_set ("error_append_string", "#RKQEND#\n");
 
+function write_to_socket ($string) {
+	global $_rk_client_socket;
+	socket_write ($_rk_client_socket, $string);
+	return "";
+}
+
+// set up a master server socket
+$_rk_master_socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
+socket_bind ($_rk_master_socket, '127.0.0.1', 0);
+socket_getsockname ($_rk_master_socket, $_rk_master_socket_ip, $_rk_master_socket_port);
+socket_listen ($_rk_master_socket);
+socket_set_nonblock ($_rk_master_socket);
+
+// advertise our port number and a shared secret
+echo ($_rk_master_socket_port . ";" . sha1 ("bulasdkj") . "\n");	// TODO!
+ob_flush ();
+fflush (STDOUT);
+flush ();
+
+// listen for connections
 while (1) {
-	eval (getInput ("requesting code"));
-	fflush (STDOUT);
+	pcntl_waitpid (-1, $_rk_waitpid_status, WNOHANG);
+	$_rk_client_socket = @socket_accept ($_rk_master_socket);
+	if ($_rk_client_socket !== false) {
+		$_rk_pid = pcntl_fork();
+		if ($_rk_pid == -1) {
+			die('could not fork');
+		} else {
+			if ($_rk_pid) {	// parent
+				socket_close ($_rk_client_socket);
+			} else {	// child
+				socket_close ($_rk_master_socket);
+				socket_set_block ($_rk_client_socket);
+				ob_start ("write_to_socket");
+				while (1) {
+					eval (getInput ("requesting code"));
+					ob_flush ();
+				}
+				exit ();
+			}
+		}
+	} else {
+		usleep (10000);
+	}
 }
+
 ?>
\ No newline at end of file
