If it appears, by affidavit, to the satisfaction of the court, that the person required to be produced is imprisoned within the state, for any cause, except upon a sentence for a felony, or is kept or detained, within the state, by any person, the court may, either before or after making the order for production, issue a writ of habeas corpus to bring him before it, or before the referee, as the case requires. The writ must be served and executed, and disobedience thereto may be punished, as where a writ of habeas corpus is issued, to inquire into the cause of the detention of a prisoner.
N.Y. Real Prop. Acts. Law § 1106