The court, upon the petition of any patient after his confinement pursuant to this subchapter for a period in excess of three months, shall inquire into the health and general condition of the patient and as to the necessity, if any, for his continued confinement. If the court finds, with or without a hearing, that his continued confinement is no longer necessary or desirable, it shall order the patient released from confinement and returned to the court. The court may, with respect to any such patient so returned, place such patient under a posthospitalization program in accordance with the provisions of section 687(a) of this title.