You can abort a connection (close it with an error) with:

void SAP_API RfcAbort(RFC_HANDLE handle, char * text)

Thus this function sends an error message, if possible, and closes the connection.

A given error message cannot be sent if the receiver is not in state where it expects to receive some RFC data.

This function is defined in SAPRFC.H.

Function Parameters:

Handle to RFC connection. If handle is RFC_HANDLE_NULL, all connections are aborted.

Error message.
If you supply a text, the text is used as error message on the receiving side. If the text field is NULL, the connection is only closed.