A judge has allowed former Vancouver real estate developer Tarsem Singh Gill to withdraw his guilty pleas in connection with a $40-million mortgage fraud.

In a ruling Friday, B.C. Supreme Court Justice Terry Schultes said that the possibility of a â€śmiscarriage of justiceâ€ť loomed large if he denied Gillâ€™s application to withdraw his pleas to two counts of fraud.

Itâ€™s the latest twist in one of B.C.â€™s longest-running criminal cases.

Gill has had five lawyers and five trial dates since the charges were laid against him in 2008. That followed a six-year police investigation into a fraudulent scheme involving dozens of home purchases dating back more than 12 years.

Kevin McCullough, Gillâ€™s latest lawyer, argued that his client was depressed and anxious at the time he entered the guilty pleas last May.

And he said Gill was not admitting to the essential elements of the offences to which he had pleaded guilty.

Prosecutor Kevin Gillett argued that Gillâ€™s pleas were a straightforward acceptance of the basic elements of the offence, put in a simple form.

He said that evidence from a psychologist about Gillâ€™s mental state was at odds with Gillâ€™s own evidence during the court application.

But the judge said despite inconsistencies about the evidence of Gillâ€™s depression, the evidence of his wife and friends about his depression at the time of the pleas was unchallenged.

His claim not to have admitted the necessary intent for fraud at the time of his guilty pleas was circumstantially supported and there was a â€śmeaningful concernâ€ť about the validity of the pleas, the judge concluded.

â€śThe possiblity of a miscarriage of justice looms large if Mr. Gill were to be held to the pleas.â€ť

The case will next be before the courts March 7 to set a date for trial.

Gillâ€™s co-accused, former lawyer Martin Wirick, pleaded guilty in June 2009 and received a seven-year jail term. He was also disbarred. The scam involved the biggest legal fraud in Canadian history.

During Wirickâ€™s sentencing, court heard that Wirick acted as Gillâ€™s solicitor and that from 1999 to 2002 misdirected money from Gill-related real estate transactions.

Instead of paying off mortgages and liens on properties that unsuspecting people thought they were buying, Wirick funnelled the money to Gill or his numbered companies. The mortgages on the homes remained. The owners of 77 properties were affected.